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?

[190 byte] By [kneelmara] at [2007-11-27 10:53:04]
# 1

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

petes1234a at 2007-7-29 11:41:48 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

jezzica85a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

petes1234a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 4

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?

kneelmara at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 5

and what is flushing? does it mean removing the previous data to allow the new data to be processed?

kneelmara at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

jezzica85a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 7

hi,

as per my experience, you can't count on that...

:)

gaurav_abbia at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 8

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

kneelmara at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 9

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.

jezzica85a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 10

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.

petes1234a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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.

petes1234a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

ejpa at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 13

> To clarify this, the operating system will always

> close files.....

Many thanks.

petes1234a at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...
# 14

thank you guys... :)

kneelmara at 2007-7-29 11:41:49 > top of Java-index,Java Essentials,Java Programming...