Using BufferedReader:
- import java.io.File;
- import java.io.FileReader;
- import java.io.BufferedReader;
- public class ReadFileByBufferedReader {
- public static void main(String[] args) throws Exception {
- File file = new File("D://data.txt");
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- StringBuffer sb = new StringBuffer();
- String line = br.readLine(); // read the line
- while (null != line) {
- sb.append(line); // append the line
- sb.append("\n");
- line = br.readLine(); // read the line
- }
- System.out.println(sb.toString());
- fr.close();
- }
- }
Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line
irrespective of amount of data that a file is having.
No comments:
Post a Comment