Java I/O Character Streams

Character Streams Overview

  • The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.
  • For most applications, I/O with character streams is no more complicated than I/O with byte streams. Input and output done with stream classes automatically translate to and from the local character set. A program that uses character streams in place of byte streams automatically adapts to the local character set and is ready for internationalization — all without extra effort by the programmer.

Using Character Streams

All character stream classes are descended from Reader and Writer.
java.io.Reader (implements java.io.Closeable, java.lang.Readable)
  • java.io.BufferedReader
  • java.io.LineNumberReader
  • java.io.CharArrayReader
  • java.io.FilterReader
  • java.io.PushbackReader
  • java.io.InputStreamReader
  • java.io.FileReader
  • java.io.PipedReader
  • java.io.StringReader
java.io.Writer (implements java.lang.Appendable, java.io.Closeable, java.io.Flushable)
To demonstrate how character streams work, we'll focus on character stream classes that specialize in a file I/O: FileReader and FileWriter.
The below examples illustrates these classes.

FileWriter Example

package com.javaguides.javaio.fileoperations.examples;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWritterExample {
 public static void main(String[] args) {
  File file = new File("C:/Project_Work/workspace/java-io-guide/sample.txt");
  String content = "This is the text content";

  try (FileWriter fop = new FileWriter(file)) {

   // if file doesn't exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
   fop.write(content);

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
The most important difference between using FileOutputStream and FileWritter is 
  • In FileWritterExample the int variable holds a character value in its last 16 bits; 
  • In FileOutputStreamExample, the int variable holds a byte value in its last 8 bits.
Read more about FileWriter on FileWriter Class in Java

FileReader Example

The FileReader example is demonstrated in FileReader Class in Java

Character Streams that Use Byte Streams

  • Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. FileReader, for example, uses FileInputStream, while FileWriter uses FileOutputStream.
  • There are two general-purpose byte-to-character "bridge" streams: InputStreamReader and OutputStreamWriter. Use them to create character streams when there are no prepackaged character stream classes that meet your needs. The sockets lesson in the networking trail shows how to create character streams from the byte streams provided by socket classes.

Line-Oriented I/O

In line-oriented, I/O, 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.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class CopyLines {
    public static void main(String[] args) throws IOException {

        BufferedReader inputStream = null;
        PrintWriter outputStream = null;

        try {
            inputStream = new BufferedReader(new FileReader("xanadu.txt"));
            outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                outputStream.println(l);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}
  • Invoking readLine returns a line of text with the line.
  • This example outputs each line using println, which appends the line terminator for the current operating system. This might not be the same line terminator that was used in the input file.

Comments