In this tutorials we are going to see how to read file using BufferedReader.
Create project called JavaMisc.
Create package called com.javatutorialscorner.io Create java class called FileReaderExample under com.javatutorialscorner.io package.
FileReaderExample.java
package com.javatutorialscorner.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("C:\\jtc\\javatutorialscorner.txt");
br = new BufferedReader(fr);
String tempLine;
while ((tempLine = br.readLine()) != null) {
System.out.println(tempLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Now run the program see the following output in console.
this is test file created by java tutorials cornerThis content read from input file
0 comments:
Post a Comment