3.7 Character Streams
3.7 Character Streams
In Java, characters are stored using Unicode conventions. Character stream automatically allows us to
read/write data character by character. For example FileReader and FileWriter are character streams used to read
from source and write to destination
The character stream classes are also topped by two abstract classes Reader and Writer.
Two kinds of Character Stream classes - Reader classes and Writer classes.
● Reader Classes
These classes are subclasses of an abstract class, Reader and they are used to read characters from a
source(file, memory or console).
● Writer Classes
These classes are subclasses of an abstract class, Writer and they used to write characters to a
destination(file, memory or console).
1. Reader classes are used to read 16-bit Unicode characters from the input stream.
2. The Reader class is the superclass for all character-oriented input stream classes.
3. All the methods of this class throw an IO Exception.
4. Being an abstract class, the Reader class cannot be instantiated hence its subclasses are used.
READER STREAM
Java Reader is an abstract class for reading character streams. The only methods that a subclass must
implement are read(char[], int, int) and close(). Some of the implementation class are BufferedReader,
CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader.
Java FileReader class is used to read data from the file. It returns data in byte format like
FileInputStream class.It is character-oriented class which is used for file handling in java.
In this example, we are reading the data from the text file testout.txt using Java FileReader class.
package com.javatpoint;
import java.io.FileReader;
public class FileReaderExample
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
Here, we are assuming that you have following data in "testout.txt" file:
Welcome to java.
Output:
Welcome to java.
Java BufferedReader
Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read
data line by line by readLine() method. It makes the performance fast. It inherits Reader class.
In this example, we are reading the data from the text file testout.txt using Java BufferedReader class.
package com.javatpoint;
import java.io.*;
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("D:\\testout.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}
Here, we are assuming that you have following data in "testout.txt" file:
Welcome to java.
Output:
Welcome to java.
WRITER STREAM
It is an abstract class for writing to character streams. The methods that a subclass must implement are
write(char[], int, int), flush(), and close(). Most subclasses will override some of the methods defined here to
provide higher efficiency, functionality or both.
Methods of Writer Class
Java FileWriter
Java FileWriter class is used to write character-oriented data to a file. It is character-oriented class which is
used for file handling in java. Unlike FileOutputStream class, you don't need to convert string into byte array
because it provides method to write string directly.
In this example, we are writing the data in the file testout.txt using Java FileWriter class.
package com.javatpoint;
import java.io.FileWriter;
public class FileWriterExample
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("D:\\testout.txt");
fw.write("Welcome to javaTpoint.");
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Success...");
}
}
Output:
Success...
testout.txt:
Welcome to java.
Java BufferedWriter
Java BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It
inherits Writer class. The buffering characters are used for providing the efficient writing of single arrays,
characters, and strings.
Let's see the simple example of writing the data to a text file testout.txt using Java BufferedWriter.
package com.javatpoint;
import java.io.*;
public class BufferedWriterExample
{
public static void main(String[] args) throws Exception
{
FileWriter writer = new FileWriter("D:\\testout.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Welcome to javaTpoint.");
buffer.close();
System.out.println("Success");
}
}
Output:
success
testout.txt:
Welcome to java.