If you want to read a file content using java, following code sample will help you to achieve this purpose.I have further describe the steps which help you to understand what this code does
- You need to create an object from Buffered Reader class.
- Specify the file location which you need to read.
- If file has some content, you are reading the file until it becomes null.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
public class RD {
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"/home/AA/IdeaProjects/Microsoft1/out/production/Microsoft1/HelloWorld.txt"));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
// read next line
line = reader.readLine();
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output