• Post Reply Bookmark Topic Watch Topic
  • New Topic

when using Printwriter object, when does the object "save" the appended text?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code:



when does the printwriter hits "save"/commit changes on the actual file (just like hitting save button on a Notepad/Ms Word)?

Is it on the instance I execute println("----------------------------------------------") or when executing the close() method?
It is because I am trying to create a log file using Writer classes(please do not suggest me to use log4j). And the above code is inside a method on "Logger" class which is being invoked by doPost and doGet methods of controller classes. My problem is that I am not sure if the file saving might override/overlap in case two users invoked the same doPost/doGet method at the same time.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think - emphasis on the 'i'm not sure' part - is that you don't know. It does it when it is convenient.

Writing to the disk can be time consuming, so I believe it tries to save up until it is convenient to do the writing. The head is near, the disk is rotated to the right spot, etc. And then you may have thread issues if two things are competing to write to the same file/location.

I believe - again, I'm not sure - you can call the flush() method to force the write at that spot in the code - but that won't nec. fix thread issues.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if writer.close() automatically call flush as well?

But I recall flush needs to be explicitly called, else the content is not written/appended to the file.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good practice would be to flush() before closing the Printwriter.

Closing the FileWriter before closing the Printwriter would be a bad idea because it does NOT automatically flush before close.

I suspect it could truncate the file. Do some experiments and let us know.

Bill
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
close() should flush the contents of a writer, as that is what's documented in the API.
Closes the stream, flushing it first.

That said, that works for the same writer. In this case, closing the FileWriter will not flush the PrintWriter. If that still has the content in its internal cache, nothing may be written out. That's one of the reasons why you should always close the outer-most writer; that should close all of the wrapped writers as well.

Note that you can create a PrintWriter so it flushes upon calling println, printf or format. Just check the constructors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!