Input and output streams are byte-based streams.
Readers and writers are character-based streams.
A reader reads character-based data from a data source. A writer writes character-based data to a data sink.
The following table lists Classes in Byte-based and Character-based Input Streams
Byte-based Input Stream Class | Character-based Input Stream Class |
---|---|
InputStream | Reader |
ByteArrayInputStream | CharArrayReader |
StringBufferInputStream | StringReader |
PipedInputStream | PipedReader |
FileInputStream | FileReader |
No corresponding class | InputStreamReader |
FilterInputStream | FilterReader |
BufferedInputStream | BufferedReader |
PushbackInputStream | PushbackReader |
DataInputStream | No corresponding class |
ObjectInputStream | No corresponding class |
The following table compares Classes from Byte-based Output Streams and Character-based Output Streams
Byte-based Output Stream Class | Character-based Output Stream Class |
---|---|
OutputStream | Writer |
ByteArrayOutputStream | CharArrayWriter |
No corresponding class | StringWriter |
PipedOutputStream | PipedWriter |
FileOutputStream | FileWriter |
No corresponding class | OutputStreamWriter |
FilterOutputStream | FilterWriter |
BufferedOutputStream | BufferedWriter |
DataOutputStream | No corresponding class |
ObjectOutputStream | No corresponding class |
PrintStream | PrintWriter |
Use the InputStreamReader class if you have a stream that supplies bytes and you want to read characters by getting those bytes decoded into characters.
Create a Reader object from an InputStream object using the platform default encoding
Reader reader = new InputStreamReader(iso);
Create a Reader object from an InputStream using the "US-ASCII" encoding
Reader reader = new InputStreamReader(iso, "US-ASCII");
You can create a Writer object to spit out characters from a bytes-based output stream, assuming that oso is an OutputStream object:
Create a Writer object from OutputStream using the platform default encoding
Writer writer = new OutputStreamWriter(oso);
Create a Writer object from OutputStream using the "US-ASCII" encoding
Writer writer = new OutputStreamWriter(oso, "US-ASCII");