Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

CharacterStream Classes in Java

In this tutorial, we will earn about CharacterStream classes in Java. These classes were introduced to overcome the drawbacks of ByteStream classes. ByteStream class can only handle 8-bit bytes and it is not compatible with the Unicode characters. On the other hand, CharacterStream classes are used to work with 16-bit Unicode characters.

In this article, we will take a look at the overview of CharacterStream classes in Java. In the next tutorials, we will cover each method in detail.

There are two classes The Reader and Writer the superclasses of all the character stream classes. Both classes are abstract classes and located in java.io package.

Java Reader Classes

These classes support reading 16-bit characters from the input stream. Reader class is an abstract class so we cannot use it directly but the following classes given in the table inherit the Reader class and overrides all the methods of this class.

All the subclasses of the Reader class is given in below:

Class Description
BufferedReader This class provides methods to read characters from the buffer.
CharArrayReader This class provides methods to read characters from the char array.
FileReader This class provides methods to read characters from the file.
FilterReader This class provides methods to read characters from the underlying character input stream.
InputStreamReader This class provides methods to convert bytes to characters.
PipedReader This class provides methods to read characters from the connected piped output stream.
StringReader This class provides methods to read characters from a string.

Methods of Reader Class

All the methods of Reader class is given in the table below.

Method Description
int read() This method returns the integral representation of the next character present in the input. It returns -1 if the end of the input is encountered.
int read(char buffer[]) This method is used to read from the specified buffer. It returns the total number of characters successfully read. It returns -1 if the end of the input is encountered.
int read(char buffer[], int loc, int nChars) This method is used to read the specified nChars from the buffer at the specified location. It returns the total number of characters successfully read.
void mark(int nchars) This method is used to mark the current position in the input stream until nChars characters are read.
void reset() This method is used to reset the input pointer to the previous set mark.
long skip(long nChars) This method is used to skip the specified nChars characters from the input stream and returns the number of characters skipped.
boolean ready() This method returns a boolean value true if the next request of input is ready. Otherwise, it returns false.
void close() This method is used to close the input stream. However, if the program attempts to access the input, it generates IOException.

Java Writer Class

This class is used to write the characters to the output stream. These characters will be in the 16-bit Unicode format. Same as Reader class, the Writer class is also an abstract class and cannot be instantiated. Subclasses of the Writer class used to write the characters on the output stream.

All the subclasses of the Writer class are given in the table below.

Class Description
BufferedWriter This class provides methods to write characters to the buffer.
FileWriter This class provides methods to write characters to the file.
CharArrayWriter This class provides methods to write the characters to the character array.
OutpuStreamWriter This class provides methods to convert from bytes to characters.
PipedWriter This class provides methods to write the characters to the piped output stream.
StringWriter This class provides methods to write the characters to the string.

Methods of Writer Class

Methods of the Writer classes are given in the table below.

Method Description
void write() This method is used to write the data to the output stream.
void write(int i) This method is used to write a single character to the output stream.
void write(char buffer[]) This method is used to write the array of characters to the output stream.
void write(char buffer [],int loc, int nChars) This method is used to write the nChars characters to the character array from the specified location.
void close () This method is used to close the output stream. However, this generates the IOException if an attempt is made to write to the output stream after closing the stream.
void flush () This method is used to flush the output stream and writes the waiting buffered characters.

Conclusion

In the tutorial, we looked into the overview of CharacterStream classes in Java. These classes belong to the java.io package. This class is introduced to overcome the drawbacks of ByteStream classes. ByteStream class can only handle 8-bit bytes and it is not compatible with the Unicode characters.