In this chapter we can learn how to read a file using bufferedreader.
BufferedReader Is a java class that reads a text from an input stream by buffering characters that seamlessly reads characters, arrays or lines and FileReader class read file.
objReader = new BufferedReader(new FileReader("test.txt"));
Example:
JavaApplication.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class JavaApplication {
public static void main(String[] args) {
BufferedReader objReader = null;
try {
String strCurrentLine;
objReader = new BufferedReader(new FileReader("test.txt"));
while ((strCurrentLine = objReader.readLine()) != null) {
System.out.println(strCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (objReader != null) {
objReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
objReader.close() Method is called to release the memory.
Example will read a text file and show on the console