Monday, April 30, 2018

Read file using BufferedReader !!

Using BufferedReader:
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;

  4. public class ReadFileByBufferedReader {

  5. public static void main(String[] args) throws Exception {
  6. File file = new File("D://data.txt");
  7. FileReader fr = new FileReader(file);
  8. BufferedReader br = new BufferedReader(fr);
  9. StringBuffer sb = new StringBuffer();

  10. String line = br.readLine(); // read the line
  11. while (null != line) {
  12. sb.append(line); // append the line
  13. sb.append("\n");
  14. line = br.readLine(); // read the line
  15. }
  16. System.out.println(sb.toString());
  17. fr.close();
  18. }
  19. }
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

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...