Read Data from a Text File using Java

Language

Today, I will teach you how to read data from a text file using Java. We're done with creating files and writing/appending files, so now we will proceed to reading of data from any files but here we just use the text files or .txt file extension. So, now let's start this tutorial! 1. Open Notepad. Put any data in there, for example, i have write "Sourcecodester is the best!". Save it to the same folder with your java program and named it as data.txt. 2. Open JCreator or NetBeans and make a java program with a file name of readFiles.java. 3. Import java.io package. Hence we will use an input/output in creating files.
  1. import java.io.*;
4. In your main, initialize the data.txt that you have created a while ago. Initialize also StringBuffer contents and BufferedReader reader to null.
  1. File file = new File("data.txt");
  2. StringBuffer contents = new StringBuffer();
  3. BufferedReader reader = null;
The StringBuffer class here is used to represent characters that can be changed or modified and the BufferedReader class here means that it reads from the input source into a buffer before passing it to another string. 5. Now, create a try and catch method. In your try method, do the following code. This will trigger to read all the data lines of data.txt.
  1. try
  2. {
  3. reader = new BufferedReader(new FileReader(file));
  4. String text = null;
  5.  
  6. // repeat until all lines is read
  7. while ((text = reader.readLine()) != null)
  8. {
  9. contents.append(text)
  10. .append(System.getProperty(
  11. "line.separator"));
  12. }
  13. }
In your Catch method, prefer to catch IOException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug. And also the FileNotFoundException if the file is not found under the same folder of the program.
  1. {
  2. e.printStackTrace();
  3. } catch (IOException e)
  4. {
  5. e.printStackTrace();
  6. }
6. Lastly, display the contents of the data.txt file using toString method.
  1. System.out.println(contents.toString());
Output: output Here's the full code of this tutorial:
  1. import java.io.*;
  2. public class readFiles
  3. {
  4. public static void main(String[] args)
  5. {
  6. File file = new File("data.txt");
  7. StringBuffer contents = new StringBuffer();
  8. BufferedReader reader = null;
  9.  
  10. try
  11. {
  12. reader = new BufferedReader(new FileReader(file));
  13. String text = null;
  14.  
  15. // repeat until all lines is read
  16. while ((text = reader.readLine()) != null)
  17. {
  18. contents.append(text)
  19. .append(System.getProperty(
  20. "line.separator"));
  21. }
  22. {
  23. e.printStackTrace();
  24. } catch (IOException e)
  25. {
  26. e.printStackTrace();
  27. }
  28.  
  29. // show file contents here
  30. System.out.println(contents.toString());
  31. }
  32. }
Hope this program helps! :) Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment