Java BufferedReader Example
BufferedReader is the class which is there in java.io package.
Buffered Reader reads the text from a character-input stream. We will pass the FileReader
as constructor argument to the Buffered Reader.
BufferedReader reads the file line by line, character, group of characters.
Create one .txt file under C directory.
I am giving below very straight forward example which reads the file content line by line and print
in the console.
While iterating to the file, if the pointer reaches to the end of the file, the readLine() method
which return the null value.
BufferedReader Example
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
//Read the file using Buffered reader.
public class Java_Bufferedreader {
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
File file = null;
String line = null;
try {
file = new File("C:\\core.txt");
fr = new FileReader(file);
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null){
fr.close();
fr = null;
}
if (br != null){
br.close();
br = null;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
0 comments:
Post a Comment