You are here: Home / Topics / Program to read data from text file using FileReader in Java

Program to read data from text file using FileReader in Java

Filed under: Java on 2024-03-01 06:44:32

// Program to read data from text file using 
// FileReader  and display it on the monitor.

import java.io.*;

class FileReaderDemo
{
public static void main( String args[ ] ) throws IOException
{
 // attach file to FileReader
 FileReader fr = null;
 try
 {
  fr = new FileReader( "file1.txt");
 }
 catch( FileNotFoundException fnfe )
 {
  System.out.println(" File Not found.");
  return;
 }
 
 // read character from FileReader.
 int ch;
 System.out.println(" File Contents are : ");
 while( (ch = fr.read()) != -1 )
 {
  System.out.print((char)ch);
 }

 // close the file.
 fr.close();
}
}


Output:

File Contents are : 
Learn Java Programming from www.example.com

About Author:
G
Geetam     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.