- We can read java file using buffered reader class in java
- We need to import java.io.BufferedReader in order to read java text file.
- Create object of java.io.BufferedReader class by passing new FileReader("C:\\Sample.txt") object to the constructor.
- In order to read line by line call readLine() method of BufferedReader class which returns current line. Initially first line of java text file
Program #1: Write a java program to read a file line by line using BufferedReader class
- package com.instanceofjava.javareadfile;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class ReadFileBufferedReader {
- /**
- * @Website: www.instanceofjava.com
- * @category: How to read java read file line by line using buffered reader
- */
- public static void main(String[] args) {
- BufferedReader breader = null;
- try {
- String CurrentLine;
- breader = new BufferedReader(new FileReader("E://Sample.txt"));
- while ((CurrentLine = breader.readLine()) != null) {
- System.out.println(CurrentLine);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (breader != null)
- breader.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Output:
- Java open and read file
- Read file line by line in java
- Example java program to read a file line by line
- java read file line by line example
Program #2: Write a java program to read a file line by line using BufferedReader class using Eclipse
No comments