This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FilterOutputStream vs Writer" Watch "FilterOutputStream vs Writer" New topic

FilterOutputStream vs Writer

steven brown

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

Joined: Aug 03, 2001
Posts: 64
Originally posted by steven brown:
Given that both can be used for writing character streams, why would you choose one over the other?

Good question! In, 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:
subject: FilterOutputStream vs Writer
It's not a secret anymore!