πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Character Stream in Java IO  


Introduction
  • A character stream is a flow of data that handles input and output of data in characters (16-bit Unicode units).
  • Character streams are used for performing high-level input and output operations specifically for text data, such as:
    • Text files (.txt)
    • XML files (.xml)
    • JSON files (.json)
    • CSV files (.csv)
  • How Character Stream Works?
    • Character streams read and write data as Unicode characters.
    • Each character typically represents 16 bits (2 bytes) of data.
    • This ensures proper handling of text data and supports internationalization.
    • Examples:
      • Reading a text file: H β†’ 'H' (character representation)
      • Writing a Unicode character: 'ΰ€…' β†’ stored correctly in file

Hierarchy of Character Streams:
  • Character streams in Java are divided into two main categories:
    1. Reader (for reading characters)
    2. Writer (for writing characters)
  • Below is the hierarchy:- Character Stream Reader Writer Hierarchy
  • The read() method is common to all Reader classes, and the write() method is common to all Writer classes. Together, they form the basic building blocks of character stream I/O in Java.

Program for Reader (FileReader):
  • Below is the program to read file using FileReader:
  • import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFileUsingReader
    {
        public static void main(String[] args)
        {
            try
            {
                // Open file for reading
                FileReader fr = new FileReader("d://aaa.txt");
                int i;
                
                System.out.println("----- Reading data from file using FileReader -----");
                while ((i = fr.read()) != -1)
                {
                    System.out.print((char) i); // read character by character
                }
                
                fr.close();
            }
            catch (IOException e)
            {
                System.out.println("Error reading file: " + e.getMessage());
            }
        }
    }
  • Make sure that there should be aaa.txt file in D drive (d://).
Program for Writer (FileWriter):
  • Below is the program to write text in file using FileWriter:
  • import java.io.FileWriter;
    import java.io.IOException;
    
    public class WriteFileUsingWriter
    {
        public static void main(String[] args)
        {
            try
            {
                // Open file for writing
                FileWriter fw = new FileWriter("d://bbb.txt");
                
                String data = "Hello, this is my first Java Program using FileWriter to write data in file....";
                fw.write(data); // write string directly
                
                fw.close();
                System.out.println("Data successfully written to bbb.txt");
            }
            catch (IOException e)
            {
                System.out.println("Error writing file: " + e.getMessage());
            }
        }
    }
  • Make sure that the path provided should be correct.