How to read a file in java

  • by

There are many ways to read file in java but simplest and common way to read file in java is BufferedReader method. The following code shows how to read file in java using BufferedReader method

Read file in java:-

import java.io.*;
public class Main
{
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new FileReader(“c:\\filename”));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(str);
}
System.out.println(str);
}

catch (IOException e)
{
}
}
}
}

Leave a Reply