How to Read from the File using FileReader class and BufferedReader class in Core Java

In this tutorial how to read from the file using FileReader class and BufferedReader class in Core Java  is shown.

Code:

FileReaderDemo.java

import java.io.*;

class Test2
{
public static void main(String args[])
{
String fileName=”temp.txt”;
String line=null;

try
{
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);

while((line=bufferedReader.readLine())!=null)
{
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex)
{
System.out.println(“Unable to open file ‘”+fileName+”‘”);
}
catch(IOException ex)
{
System.out.println(“Error reading file ‘”+fileName+”‘”);
}
}
}

Add a Comment

Your email address will not be published.