Monday 7 October 2013

Read File Using Files Class,BufferedReader



import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class BufferedReaderWithFilesExample {

public static void main(String args[]) {

Charset cs= StandardCharsets.UTF_8;
Path path = Paths.get("d:/ram.txt");
        BufferedReader br=null;
try {
br = Files.newBufferedReader(path,cs);
String line;
       while((line = br.readLine()) != null){
           //process the line
           System.out.println(line);
       }
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

No comments:

Post a Comment