Sunday, April 29, 2018

Read data from a file using FileReader class !!

Read file using FileReader:
  1. import java.io.File;
  2. import java.io.FileReader;

  3. public class ReadFileByFileReader {
  4. public static void main(String[] args) {
  5. try {
  6. File file = new File("D:\\data.txt");
  7. FileReader fr = new FileReader(file);

  8. int length = (int) file.length();
  9. char c[] = new char[length];

  10. fr.read(c);
  11. String data = new String(c);

  12. System.out.println(data);
  13. fr.close();
  14. } catch (Exception e) {
  15. System.out.println("excepton occured in " + e);
  16. }
  17. }
  18. }
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...