Originally posted by steven brown: Hi, Given that both can be used for writing character streams, why would you choose one over the other? Regards, Steven
Good question! In java.io, almost anything with "stream" in its name is designed for reading raw data (bytes, binary data) whereas any Reader or Writer is designed for handling text, and will do "character set encoding" (mapping between Unicode inside Java and some character set such as USASCII, Spanish, Swedish, Farsi, Kanji, etc.). A good reference for this is the book "Java I/O" by Elliotte Rusty Harold. There's a chapter with some examples in my Java Cookbook too, of course, but if you want all the details, get Rusty's book. ------------------ Ian Darwin, Author of Java Cookbook: Solutions and Examples for Java Developers
The I/O chapter in Core Java inspired me to be intersted in streams and readers and writers when it discussed RandomAccessFile and the fact that RandomAccessFile implements DataInput and DataOutput. If you make parameters of type DataInput or DataOutput for methods accessing a RandomAccessFile , you can use the same methods to write to a stream, such as writing to a Socket's getInputStream(). That led me to learning how to buffer a stream, but keeping the "stream" object on the outside, so it could use the methods made for DataInput or DataOutput. The IO stuff is pretty complicated, and I had to come at it from a lot of different directions, and with a particular problem to solve (reading and writing to RandomAccesFiles) The book Java Networking, also by o'reilly has a great chapter on I/O.