ch04-Data Streams(3)

Download Report

Transcript ch04-Data Streams(3)

Data streams(3)
Readers and Writers
Readers and Writers




java.io.Reader class: a character-based
equivalent of the java.io.InputStream class.
Character-oriented
A better alternative than input streams and
output streams when used on text data.
To better support the Unicode character
streams.
What are Unicode characters?

ASCII characters
-

Unicode characters
-

composed of 8 bits
offering a range of 256 possible characters
presented by 16 bits
allowing for a maximum of 65536 possible characters
UTF-8
-
a modified form of Unicode encoding
A variable-width encoding format; some characters are a
single byte and others multiple bytes.
The java.io.Reader class
The java.io.Reader class

Methods

void close()

int read()
-

int read(char[] characterArray)
-

reads and returns a chracter.
populates an array of characters with data.
boolean ready()
- returns “true” if there is data available.

……
The java.io.Reader class

Low-level readers






CharArrayReader
FileReader
PipedReader
StringReader
InputStreamReader
Filter readers


BufferedReader
…
Low-level Readers
Low-level readers*

CharArrayReader


FileReader


Reads a sequence of characters from a thread
communication pipe, like a PipedInputStream
StringReader


Reads from a file, just like a FileInputStream
PipedReader


Reads from a character array
Reads a sequence of characters from a string, as if it
were a StringBufferInputStream
InputStreamReader

Bridges the divide between an input stream and a reader,
by reading from the input stream.
CharArrayReader*

Constructors

CharArrayReader(char[] charArray)
-

creates a character array reader that will
operate on the specified array.
CharArrayReader(char[] charArray, int
offset, int length)
FileReader*

Constructors

FileReader(File file)
- creates a reader that will access the contents of the
specified file object.

FileReader(String filename)
- … the specified filename

FileReader(FileDescriptor descriptor)
- … the specified descriptor handle
PipedReader*

Constructors

PipedReader()
- creates an unconnected pipe reader

PipedReader(PipedWriter writer)
- creates a connected pipe that will read the output of
the specified writer

Methods

void connect(PipedWriter writer)
- connects the reader to the specified writer
StringReader*

Constructor

StringReader(String stringToBeRead)
- reads from the beginning of the specified string until
the end
InputStreamReader

Constructors

InputStreamReader(InputStream input)
- connects an input stream to the reader

InputStreamReader(InputStream input, String encoding)
- … using the specified encoding form

Methods

String getEncoding()
- returns the name of the character encoding used by this stream
Filter readers
BufferedReader

Constructors

BufferedReader(Reader reader)
- reads data from the specified reader into a buffer


BufferedReader(Reader reader, int bufferSize)
Methods

String readLine()
- reads a line of text form the underlying stream.
The line is terminated by a line separator, such
as a carriage return/linefeed.
Code for
InputStreamToReaderDemo
The java.io.Writer class
The java.io.Writer class

Methods


void close()
void flush()
- flushes any unsent data.





void
void
void
void
void
write(int character)
write(char[] charArray)
write(char[] charArray, int offset, int length)
write(String string)
write(String string, int offset, int length)
The java.io.Writer class

Low-level writers






CharArrayWriter
FileWriter
PipedWriter
StringWriter
OutputStreamWriter
Filter writers



BufferedWriter
PrintWriter
……
Low-level Writers
Low-level writers*

CharArrayWriter


FileWriter


Writes characters to a thread communication pipe
StringWriter


Writes to a local file
PipedWriter


Writes to a variable length character array (and resizes
as characters are added)
Writes characters to a string buffer
OutputStreamWriter

Writes to a legacy output stream
CharArrayWriter*

……
Code for
OutputStreamToWriterDemo
Filtered Writers
BufferedWriter

Constructors

BufferedWriter(Writer writer)
- creates a buffered writer, connected to a specified
writer

BufferedWriter(Writer writer, int bufferSize)
- … with a buffer of the specified size

Methods

No methods
PrintWriter


The sister class of the PrintStream, and provides
the same methods for writing datatypes as text.
Constructors

PrintWriter(Writer writer)
- creates a print writer, writing to the specified writer

PrintWriter(Writer writer, boolean flushFlag)
- auto-flush whenever a println method or a line separator is sent

Methods


void print()
void println()