Java Program to Read Char by Char from a File

In this tutorial, you will learn how to Read Char by Char from a File in java. To understand it better, you may want to check the following first:


Java Program to Read Char by Char from a File

First, save the content in a text file with the same name that you enter in a program.
For this example, a text file saved with the name javaCode.txt

javaCode.txt

Welcome to Simple2Code program

//read char by char from a file 

 import java.io.*;

 public class ReadCharByChar
 {
  public static void main(String[] args) throws IOException 
  {
    File f = new File("javaCode.txt");   
    
    FileReader fr=new FileReader(f);  
    BufferedReader br=new BufferedReader(fr);
      
    int c = 0;             
    
    while((c = br.read()) != -1)  //Read char by Char
    {
      char character = (char) c;    //converting integer to char
      System.out.println(character); //Display
    }
      
  }
 }

Output:

 W
 e
 l
 c
 o
 m
 e

 t
 o

 S
 i
 m
 p
 l
 e
 2
 C
 o
 d
 e

 p
 r
 o
 g
 r
 a
 m