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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 |