One of the limitations of byte stream classes is that it can handle only 8-bit bytes and cannot work directly with Unicode characters. To overcome this limitation, character stream classes have been introduced in java.io package to match the byte stream classes. The character stream classes support 16-bit Unicode characters, performing operations on characters, character arrays, or strings, reading or writing buffer at a time. Character stream classes are divided into two stream classes namely, Reader class and Writer class.
We’ll be covering the following topics in this tutorial:
Reader Classes
Reader classes are used to read 16-bit unicode characters from the input stream. The Reader class is the superclass for all character-oriented input stream classes. All the methods of this class throw an IOException. Being an abstract class, the Reader class cannot be instantiated hence its subclasses are used. Some of these are listed in Table
Table Reader Classes
Class | Description |
BufferedReader | contains methods to read characters from the buffer |
CharArrayReader | contains methods to read characters from a character array |
FileReader | contains methods to read from a file |
FilterReader | contains methods to read from underlying character-input stream |
InputStreamReader | contains methods to convert bytes to characters |
PipedReader | contains methods to read from the connected piped output stream |
StringReader | contains methods to read from a string |
The Reader class defines various methods to perform reading operations on data of an input stream. Some of these methods along with their description are listed in Table.
Table Reader Class Methods
Method | Description |
int read() | returns the integral representation of the next available character of input. It returns -1 when end of file is encountered |
int read (char buffer []) | attempts to read buffer. length characters into the buffer and returns the total number of characters successfully read. It returns -I when end of file is encountered |
int read (char buffer [], int loc, int nChars) | attempts to read ‘nChars’ characters into the buffer starting at buffer [loc] and returns the total number of characters successfully read. It returns -1 when end of file is encountered |
void mark(int nChars) | marks the current position in the input stream until ‘nChars’ characters are read |
void reset () | resets the input pointer to the previously set mark |
long skip (long nChars) | skips ‘nChars’ characters of the input stream and returns the number of actually skipped characters |
boolean ready () | returns true if the next request of the input will not have to wait, else it returns false |
void close () | closes the input source. If an attempt is made to read even after closing the stream then it generates IOException |
Using Buffered.Reader Class
The BufferedReader class creates a buffered character stream between the input device and the program. It defines the methods to read the data from the buffer in the form of characters.
The BufferedReader object can be created using one of the following two constructors.
BufferedReader(Reader inpStream) //first
BufferedReader(Reader inpStream, int nChars) //second
The first constructor creates a buffered character stream of default buffer size. The second constructor creates a buffered character stream that uses an input buffer of size nChars.
Besides the methods provided by the Reader class, the BufferedReader class contains some other methods which are discussed as follows.
• String readLine ():It reads a line of the input text.
• boolean ready () : It checks whether or not the stream is ready to be read.
A program to demonstrate the use of BufferedReader class
import java.io.*;
class BufferedReaderExample {
publicstaticvoid main(String args[])throwsIOException{
String s=“This program is skipping first character of each word in the string”;
StringReader sr = newStringReader(s);
System.out.println(“The input string is: “+s);
/*Creating an instance of BufferedReader using StringReader*/
BufferedReader br = newBufferedReader(sr);
//Reading from the underlying StringReader
System.out.print (“The output string is: “);
br.skip(1);
/*skipping the first character of the first word of the input string*/
//reading the remaining string
int i=0;
while((i=br.read())!=–1){
System.out.print((char)i);
if((char)i==‘ ‘)
//checking for white spaces
{
br.skip(1);
/*skipping first character of each word*/
}
}
}
}
The output of the program is
The input string is: This program is skipping first character of each word in the string
The output string is: his rogram s kipping irst haracter f ach ord n he tring In this program, skip () method is used to skip the first character of each word of the input string.
Writer Classes
Writer classes are used to write 16-bit Unicode characters onto an outputstream. The Writer class is the superclass for all character-oriented output stream classes .All the methods of this class throw an IOException. Being an abstract class, the Writer class cannot be instantiated hence, its subclasses are used. Some of these are listed in Table.
Table Writer Classes
Class | Description |
BufferedWriter | Contains methods to write characters to a buffer |
FileWriter | Contains methods to write to a file |
FilterWriter | Contains methods to write characters to underlying output stream |
CharArrayWriter | Contains methods to write characters to a character array |
OutputStreamWriter | Contains methods to convert from bytes to character |
PipedWriter | Contains methods to write to the connected piped input stream |
StringWriter | Contains methods to write to a string |
The Writer class defines various methods to perform writing operations on output stream. These methods along with their description are listed in Table.
Table Writer Class Methods
Method | Description |
void write () | writes data to the output stream |
void write (int i) | Writes a single character to the output stream |
void write (char buffer [] ) | writes an array of characters to the output stream |
void write(char buffer [],int loc, int nChars) | writes ‘n’ characters from the buffer starting at buffer [loc] to the output stream |
void close () | closes the output stream. If an attempt is made to perform writing operation even after closing the stream then it generates IOException |
void flush () | flushes the output stream and writes the waiting buffered output characters |
Using BufferedWriter Class
The BufferedWriter class, an output counterpart of the BufferedReader, is used to write characters onto the character-output stream.
The BufferedWriter object can be created using one of the following two constructors.
BufferedWriter(Writer outStream) //first
BufferedWriter(Writer outStream, int nChars) //second
The first constructor creates a buffered character stream of default buffer size. The second constructor creates a buffered character stream that uses an input buffer of size nChars. Besides the methods provided by the Writer class, the BufferedWriter class contains the newLine () method which writes a new line.
//A program to demonstrate the use of BufferedWriter class
The output of the program is
This is an example of BufferedWriter
import java.io.*;
class BufferedWriterExample {
public static void main(String args[]) throws IOException {
String s = “This is an example of BufferedWriter”;
StringWriter sw = new StringWriter();
//creating an instance of BufferedWriter class
BufferedWriter bw = new BufferedWriter(sw);
bw.write(s,0,5);
bw.newLine();
bw.write(s,5,s.length()–5);
/*writes the substring starting from index 5*/
bw.flush() ;
//flushes the stream
System.out.println(sw.getBuffer());
sw.close();
//closing the stream
bw.close();
//closing the stream
}
}
In this example the BufferedWriter class is used to write data to character-output stream (instance of StringWriter class). The getBuffer () method of the StringWriter class returns a buffer of String type.