File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes FilterOutputStream vs Writer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FilterOutputStream vs Writer" Watch "FilterOutputStream vs Writer" New topic
Author

FilterOutputStream vs Writer

steven brown
Greenhorn

Joined: Sep 19, 2001
Posts: 14
Hi,
Given that both can be used for writing character streams, why would you choose one over the other?
Regards,
Steven
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
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


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
John Kilbourne
Ranch Hand

Joined: Aug 22, 2001
Posts: 30
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FilterOutputStream vs Writer