wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes FileWriter Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FileWriter Question" Watch "FileWriter Question" New topic
Author

FileWriter Question

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

I am trying to understand how write(), flush() and close() work, however I am having some trouble understanding the concept.

Here's the code.


My expected output is:

1. fw1 is flushed first in code so(also, fw1's constructor says append is true, so it should actually append to an empty file first), here's output I am expecting
Appending fw1
Appending fw1 again
Hello Chintan
Hello World

2. This output is true if fw flushes before fw1 (which should not be possible).
Hello Chintan
Hello World
Appending fw1
Appending fw1 again

However, when I open the file to see output it comes to:
Hello Chintan
Hello Worldfw1 again


Any help much appreciated.

Thanks
Chintan.


SCJA 1.0
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

The two FileWriters work on the same file, but they don't know about each other. Therefore:
- fw empties the file, then starts at the start, increasing its own position in the file, ignoring everything that fw1 does
- fw1 starts at the end of the current file, but that has just been emptied. It also uses its own position in the file, ignoring everything that fw does.

So fw writes "Hello Chintan\nHello World" and fw1 writes "\nAppending fw1\nAppending fw1 again". Since fw is flushed later, its contents will be leading. So the combined version (I replaced \n with _ so it will also take up only one character):
The result, with fw overwriting everything fw1 did but not removing any of its data: "Hello Chintan\nHello World" followed by the remaining "fw1 again". This is what you are getting:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Wow! Great explanation! I am now able to understand how code works.

Thanks so much Rob.

Best Regards,
Chintan.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Just out of curiosity I commented out the lines that performed "flush()" and the output still remains same.

So, is it really necessary to use flush()?

If yes, then under what circumstances do we actually need to use flush()?

I have poked around close() API and couple of other threads in this forum and it says close() API flushes first and then it closes.

It looks like its behaving this way (at least in the above code ).


Thanks
Chintan.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You can use flush() to ensure the data is written immediately. You can use this to write intermediate data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileWriter Question