In this lesson we will learn How to read file in Java using BufferedReader.
BufferedReader (extends Reader) Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
Now Lets see the following Example
package read_file; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Demo { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("C:\\Path\\to\\file\\fileName.txt")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Leave a Reply