In this section we will discuss the I/O Character Streams.
Java I/O Character Streams
In this section we will discuss the I/O Character Streams.
Character streams works with the characters rather than the byte. In Java characters are stored by following the Unicode (allows a unique number for every character) conventions. In such kind of storage characters becomes the platform independent, program independent, language independent. Characters storage using Unicode convention makes the Java ready for internationalization and for this Java provides the Character stream I/O. Character stream I/O is helpful for internationalization because it automatically translated the internal format to and from the local character set.
In Java to facilitate the character stream I/O the character stream classes are defined. These classes are drawn from the Reader and Writer classes. Both of these classes are abstract classes. where the Reader class is used for input (read from) and the Writer class is used for output (write to). To work with the file I/O specialized classes are the FileReader and FileWriter.
Reader : An abstract class that facilitate to take input
(read) the character stream. Subclasses of this class must have to implement the
read(char[], int, int) and close() methods.
public abstract class Reader
extends Object
implements Readable, Closeable
Method Description
- close() : This method is used to close the resource and the
resource associated to the stream get released.
public abstract void close() throws IOException
- mark(int readAheadLimit) : This method is used to mark the
current position in the stream. This method operation is not supported by the
all character-input stream.
public void mark(int readAheadLimit) throws IOException
- markSupported() : This method specifies that whether the
mark() operation is supported by the stream or not. Default value is false.
public boolean markSupported()
- read() : This method is used to read the characters. It
reads a single character. It returns the number of read characters in the range
0 to 65535 or -1 if it reaches the end of the stream.
public int read() throws IOException
- read(char[] cbuf) : This method is used to read the
characters. Characters are read into an array. This method returns the number of
read characters or -1 if it reaches the end of stream.
public int read(char[] cbuf) throws IOException
- read(char[] cbuf, int off, int len) : This method is used
to read the characters. Characters are read into an array at specified len.
public abstract int read(char[] cbuf, int off, int len) throws IOException
- read(CharBuffer target) : This method is used to read
characters from the specified character buffer.
public int read(CharBuffer target) throws IOException
- ready() : This method specifies that is the stream ready
for reading or not.
public boolean ready() throws IOException
- reset() : This method repositioned the stream where(if) the
mark method was marked the current position in the input stream. Or reset
according to the particular stream.
public void reset() throws IOException
- skip(long n) : This method is used to skipped over the
characters in the stream.
public long skip(long n) throws IOException
Some subclasses of the Reader class are given into table :
Classes | Description |
BufferedReader | BufferedReader class provides the facility to read texts from a character-input stream using buffer. |
CharArrayReader | CharArrayReader implements the character buffer to read character stream. |
FilterReader | This class is used for reading the filtered character streams. |
InputStreamReader | This class acts as a bridge from byte streams to character streams. |
PipedReader | Piped character input stream |
StringReader | Character stream read from the String |
Writer : An abstract class that facilitate to write
character streams. Subclasses of this class must have to implement the
write(char[], int, int), flush() and close() methods.
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable
Method Description
- append(char c) : This method adds the specified character
at the end to the stream.
public Writer append(char c) throws IOException
- append(CharSequence csq) : This method adds the specified
character sequence at the end to the writer stream.
public Writer append(CharSequence csq) throws IOException
- append(CharSequence csq, int start, int end) : This method
is used to add the character to the writer stream in the subsequence of the
character sequence.
public Writer append(CharSequence csq, int start, int end) throws IOException
- close() : This method is used to close the writer stream.
public abstract void close() throws IOException
- flush() : This method is used to flush the writer stream.
public abstract void flush() throws IOException
- write(char[] cbuf) : This method is used to write the array
of characters.
public void write(char[] cbuf) throws IOException
- write(char[] cbuf, int off, int len) : This method is used
to write the specified portion of an Array of characters.
public abstract void write(char[] cbuf, int off, int len) throws IOException
- write(int c) : This method is used to write a single
character.
public void write(int c) throws IOException
- write(String str) : This method is used to write a string.
public void write(String str) throws IOException
- write(String str, int off, int len) : This method is used
to write the specified portion of string.
public void write(String str, int off, int len) throws IOException
Some subclasses of Writer class are as follows :
Classes | Description |
BufferedWriter | BufferedWriter class provides the facility to write texts to a character-output stream using buffer. |
CharArrayWriter | CharArrayWriter implements the character buffer to write character stream. |
FilterWriter | This class is used for writing the filtered character streams. |
OutputStreamWriter | This class acts as a bridge from character streams to byte streams. |
PipedWriter | Piped character output stream |
PrintWriter | This class is used to write the formatted representation of objects to a text output stream. |
StringWriter | This class writes a constructed string. String is constructed after
collecting the output of a character stream into a buffer. |