Sitemap
Lavish Jain

Lavish Jain

Software Engineer

Java I/O Character Stream Implementation

2 min readJan 12, 2022

--

Character Stream is required to read and write Unicode based text data. It reads and writes character by character (as it has capability to decode bytes into characters according to the character set used). (Please read Java I/O overview first to understand basic concept of Java I/O).
Reader and Writer are two abstract classes provided by Java that are based on Character Stream. Let’s go through some of the implementations of Reader/Writer.

InputStreamReader/OutputStreamWriter

The InputStreamReader/OutputStreamWriter class wraps a Java InputStream/OutputStream, hence converting the byte based InputStream/OutputStream into a character based Reader/Writer.

File file = new File("fileTest.txt");
OutputStream fos = new FileOutputStream(file);
Writer osw = new OutputStreamWriter(fos);
osw.write('ל'); //write(int), write(char)
osw.write("\nhello\n"); // write(String)
osw.write(new char[]{'J','a','v','a'}); // write(char[])
osw.flush();
osw.close();
fos.flush();
fos.close();
InputStream fis = new FileInputStream(file);
Reader isr = new InputStreamReader(fis);
int data = isr.read();
while(data!=-1){
System.out.print((char)data);
data = isr.read();
}
/* read(char[]) can also be used which will read complete data in char array */

FileReader/FileWriter

Used to read/write characters from/to a file.

File file = new File("fileTest.txt");
Writer fw = new FileWriter(file); //by default overwrites the file
Writer fw1 = new FileWriter(file,true); //true means append the file
Writer fw2 = new FileWriter(file,false);//false means overwrite file
//write methods are same as discussed for OutputStreamWriter

Get Lavish Jain’s stories in your inbox

Join Medium for free to get updates from this writer.

Reader fr= new FileReader(file);
//read methods are same as discussed for InputStreamReader

BufferedReader/BufferedWriter

BufferedReader read relatively large chunks of data at once and keep this data in a buffer. When you read a character from the Java BufferedReader, it is retrieved from the buffer, which minimizes the number of times that time-intensive read operations are performed.
When the buffer is fully read, the BufferedReader reads another larger block of data into the buffer.
Similarly, BufferedWriter is used to capture characters in a buffer, and write the whole buffer in one batch to an underlying Writer for increased performance.

BufferedWriter bfw = new BufferedWriter(new FileWriter("file.txt"));
//write() methods same as discussed in previous examples

BufferedReader provides an additional readLine() method which allows to read one line from buffer at a time. It returns null when there are no more lines.

//Reading line from console using BufferedReader
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String data = br.readLine();
while(data != null){
System.out.println(data);
data = br.readLine();
}

PrintWriter

The PrintWriter class enables you to write formatted data to an underlying Writer. The PrintWriter class can format primitive types like int, long etc. formatted as text

File file = new File("fileTest.txt");
FileWriter writer = new FileWriter(file,true);
PrintWriter pw = new PrintWriter(writer);
//write() method is same as before
pw.write(65);
pw.write("\nHello\n");
//below are some of the extra methods provided by PrintWriter
pw.print("Using print\n");
pw.println("Using println");
pw.format("%s is %s %d\n","Java", "Awesome",1);
pw.printf("%s is %s %d","Java", "Awesome",2);
//format and printf have exactly same implementation
pw.flush();
writer.flush();
pw.close();
writer.close();
Output (Content printed in the file)-
A
Hello
Using print
Using println
Java is Awesome 1
Java is Awesome 2

--

--