Read Data from a Text File using Java

May 30, 2020
Tutorials
read data from a text file in java

Today, I will show you how to read data from a text file using Java.

Let’s start!

1. Open Notepad. Put any data in there, for example, I have written “CampCodes 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.

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.

File file = new File("data.txt");
StringBuffer contents = new StringBuffer();
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. Create a try and catch method. In your try method, follow the code. This will trigger to read all the data lines of data.txt.

try
       {
           reader = new BufferedReader(new FileReader(file));
           String text = null;

           // repeat until all lines is read
           while ((text = reader.readLine()) != null)
           {
               contents.append(text)
                   .append(System.getProperty(
                       "line.separator"));
           }
       }

In your Catch method, prefer to catch IOException then use printStackTrace() method. It will help you on tracing 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.

catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

6. Finally, display the contents of the data.txt file using toString method.

Here’s the full code of this tutorial:

import java.io.*;
public class readFiles
{
        public static void main(String[] args)
    {
        File file = new File("data.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
 
        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
 
            // repeat until all lines is read
            while ((text = reader.readLine()) != null)
            {
                contents.append(text)
                    .append(System.getProperty(
                        "line.separator"));
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } 
 
        // show file contents here
        System.out.println(contents.toString());
    }
}
Download Code
Facebook Comments

Leave a Reply

Your email address will not be published.