Need Help on Java I/O Writer Classes
Why is it efficient to wrap an OutputStreamWriter/FileWriter on a BufferedWriter? What's with the buffer? What happens when you buffer? What does it do that makes the code efficient?
Why is it efficient to wrap an OutputStreamWriter/FileWriter on a BufferedWriter? What's with the buffer? What happens when you buffer? What does it do that makes the code efficient?
imagine moving bricks from point A to point B. You could do it one brick at a time, or you could use a wheelbarrow and efficienty move a bunch all at once. The latter is what buffering is all about. Read more about it here:
http://java.sun.com/docs/books/tutorial/essential/io/buffers.html
Also, with BufferedWriters, you don't have to worry about flushing the streams when the buffer is full, unless you're being anal and hyper-worried about losing data. It does the flushing automatically.
> Also, with BufferedWriters, you don't have to worry
> about flushing the streams when the buffer is full,
> unless you're being anal and hyper-worried about
> losing data. It does the flushing automatically.
I don't think that you can always count on this.
follow-up question:
Why is it when i dont perform writer.close(), nothing is written on the file:
Writer writer = new FileWriter("output.txt");
writer.write("Heelow");
But accourding to one of the articles ive read, java will close it for you at the end of the program...any explanations?
and what is flushing? does it mean removing the previous data to allow the new data to be processed?
Flushing means emptying the data out of the buffer and writing it to the file. I'm actually pretty sure you can count on that, at least from my own experience, but I could be wrong.
so the sequence would be: buffer, flush, write? and lastly, is there any rules/tips that i can use to determine which Writer should i use? coz i saw codes that uses PrintWriter(BufferedWriter(FileWriter)), others uses PrintWriter(FileOuputStream) or other would use FileWriter directly...which is which or when do i use each of these? assuming that im processing a character/s not binary
Hmm. I guess I'm outnumbered and you can't count on the buffer flushing. You know, maybe this will be beneficial to all of us then--when can't you count on the buffer flushing?
And keelmar, if you're making those method calls, the sequence is buffer, write, flush. I think I accidentally added too much into my earlier explanation, sorry.
If you close your buffer, it should flush (at least per the API). I place calls to flush after critical information has been printed to the stream. I also often try to close in a finally block.
> But accourding to one of the articles ive read, java
> will close it for you at the end of the program...any
> explanations?
Please show me those articles. I always close any writer that I've opened and usually in a finally block so it is almost guaranteed to close under most all circumstances.
To clarify this, the operating system will always close files.
Java only closes output streams and writers if they get garbage-collected, which may never happen. So it may never invoke the automatic flush implied by OutputStream.close() and Writer.close(). So you lose data. So you should always close them.
The only times you need to flush are when engaging in transactions, e.g. when you know you want the last write to go to the disk, or when you've written a request to the network and are about to read the response.