Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FilterOutputStream vs Writer

 
steven brown
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Kilbourne
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic