This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 19656
    
  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: 19656
    
  18

You can use flush() to ensure the data is written immediately. You can use this to write intermediate data.
 
wood burning stoves
 
subject: FileWriter Question
 
Similar Threads
File I/O R/W + Permission Issue
FileWriter
Writting into two files
Java IO question
File I/O