Friday, September 13, 2019

Character Streams in Java IO

In this Java IO Stream classes tutorial we’ll go through the Java character stream IO classes.

For Byte stream classes refer this post- Byte Streams in Java IO

Character streams in Java

In Java IO byte stream classes are used when working with bytes or binary objects same way character stream classes are used when when working with characters or Strings. In Java platform character values are stored using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set.

All character stream classes descend from Reader and Writer abstract classes.

Java Reader class

All classes in java.io package for reading character streams descend from Reader class which is an abstract class. List of classes in the hierarchy are as given below-

  • BufferedReader- This reader improves performance by buffering input while reading characters, arrays, and lines.
  • CharArrayReader- This class uses a char array as an input source.
  • LineNumberReader- This class extends BufferedReader and represent a buffered character-input stream that keeps track of line numbers.
  • InputStreamReader- An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. To improve efficiency InputStreamReader should be wrapping within a BufferedReader.
  • FileReader- It is a specialized character stream for reading character files.
  • PushbackReader- A character-stream reader that allows characters to be pushed back into the stream.This allows you to look ahead in the input stream.
  • PipedReader- Piped character-input streams which should be connected to PipedWriter to create a piped stream. Ideally separate threads should be used for PipedReader and PipedWriter.
  • StringReader- A character stream whose source is a string.

Java Writer class

All classes in java.io package for writing character streams descend from Writer class which is an abstract class. List of classes in the hierarchy are as given below-

  • BufferedWriter- Buffers characters while writing text to a character-output stream so as to provide for the efficient writing of single characters, arrays, and strings.
  • CharArrayWriter- This class uses a char array as the destination.
  • OutputStreamWriter- An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. OutputStreamWriter should be wrapped within a BufferedWriter to improve performance.
  • FileWriter- It is a specialized character stream for writing text to character files.
  • PipedWriter- Piped character-input streams which should be connected to PipedReader to create a piped stream. Ideally separate threads should be used for PipedReader and PipedWriter.
  • PrintWriter- Prints formatted representations of objects to a text-output stream.
  • StringWriter- A character stream that collects its output in a string buffer, which can then be used to construct a string.

Java character stream example

In this character stream example line-oriented I/O is used. Lines from a File are read using BufferedReader and written to a file using PrintWriter.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    BufferedReader br = null;
    PrintWriter pw = null;
    try {
      // Streams
      br = new BufferedReader(new FileReader("F:\\Temp\\abc.txt"));
      pw = new PrintWriter(new FileWriter("F:\\Temp\\write.txt"));
      String line;
      while ((line = br.readLine()) != null) {
        pw.println(line);
      }
    } finally {
      // Closing streams
      if (br != null) {
        br.close();
      }
      if (pw != null) {
        pw.close();
      }
    }
  }
}

That's all for this topic Character Streams in Java IO. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java
  2. Java Program to Convert a File to Byte Array
  3. How to Read Input From Console in Java
  4. How to Read Properties File in Java
  5. Zipping Files And Folders in Java

You may also like-

  1. How to Create PDF From XML Using Apache FOP
  2. PreparedStatement Interface in Java-JDBC
  3. Difference Between ArrayList And CopyOnWriteArrayList in Java
  4. Java Object Cloning - clone() Method
  5. Ternary Operator in Java With Examples
  6. Spring MVC Generate Response as JSON Example
  7. Spring Transaction Management JDBC Example Using @Transactional Annotation
  8. Python String isdigit() Method

No comments:

Post a Comment