Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileWriter Question

 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Great explanation! I am now able to understand how code works.

Thanks so much Rob.

Best Regards,
Chintan.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use flush() to ensure the data is written immediately. You can use this to write intermediate data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic