How to read file using BufferReader.
This program simply explains how to read file form given file path . Using bufferReader
this we can read file . Vary simple use of io and util package of java 7. You compile
this code in your machine to get output.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
**
* @author Mukesh Jha
*
*
*/
public class Bufferreader {
public static List<String> readLines(File file) throws Exception {
if (!file.exists()) {
return new ArrayList<String>();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> results = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
results.add(line);
line = reader.readLine();
}
return results;
}
public static void main (String str[]) {
System.out.println("Input your File Path");
Scanner scn=new Scanner(System.in);
File fileInput =new File (scn.nextLine());
try {
List <String> fileRead=readLines(fileInput);
for( String strm:fileRead) {
System.out.println( strm);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Programm output :
Input your File Path
/Users/yourpath/Resume9EXPJava.docx
How to read file using BufferReader.
Reviewed by Mukesh Jha
on
12:08 AM
Rating:
No comments:
Add your comment