Published using Google Docs
NETBEANS Java Basics 114 – Reading Text File
Updated automatically every 5 minutes

Java Basics 114 – Reading Text File

STEPS

1) CREATE NEW PROJECT

Project Name: textfiles

Main Class Name: textfiles.FileData

Delete all comments and unnecessary codes, leaving only the following codes.

package textfiles;

public class FileData {

    public static void main(String[] args) {

        // TODO code application logic here

    }

   

}

2) Add Import Statement.

2-1) Insert Import Statement after Package Declaration Statement.

package textfiles;

import java.io.IOException;

public class FileData {

    public static void main(String[] args) {

        // TODO code application logic here

    }

}

3) Add Exception Handling mechanism.

3-1) Add THROWS to Main Method Declaration.

package textfiles;

import java.io.IOException;

public class FileData {

    public static void main(String[] args)  throws IOException  {

        // TODO code application logic here

    }

}

The throw keyword is part of Java's exception handling.

4) Create Class to read text file.

4-1) Right-click the textfiles package and select New/Java Class.

4-2) Type Class Name ReadFile.

4-3) Startup codes.

package textfiles;

public class ReadFile {

   

}

5) Add Import Statement.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

   

}

6) Add instructions into Main Method.

6-1) Declare a String named path.

6-1) Declare a Method named ReadFile.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

}

7) Add Method to open text file.

7-1) Declare Method OpenFile.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

   

    }

}

7-2) Add instructions create file reader object and buffer object.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);

    }

}

7-3) Add instructions to create variable to store data.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;

        String[] textData = new String[numberOfLines];

    }

}

7-4) Add instructions to pass data from buffer to variable.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);

       

        int numberOfLines = 3;

        String[] textData = new String[numberOfLines];

        int i;

        for (i = 0; i < numberOfLines; i++) {

            textData[ i] = textReader.readLine();

        }

    }

}

7-5) Add instructions to close object and return the text data.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;

       

        String[] textData = new String[numberOfLines];

       

        int i;

        for (i = 0; i < numberOfLines; i++) {

            textData[ i] = textReader.readLine();

        }

        textReader.close();

        return textData;

    }

}

7-6) Add Method to read number if lines in text file.

package textfiles;

import java.io.IOException;

import java.io.FileReader;

import java.io.BufferedReader;

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {

        path = file_path;

    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;

        String[] textData = new String[numberOfLines];

        int i;

        for (i = 0; i < numberOfLines; i++) {

            textData[ i] = textReader.readLine();

        }

        textReader.close();

        return textData;

    }

    int readLines() throws IOException{

        FileReader file_to_read = new FileReader(path);

        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;

        int numberOfLines = 0;

        while ((aLine = bf.readLine()) != null) {

            numberOfLines++;

        }

       

        bf.close();

        return numberOfLines;

    }

}

8) Add instructions to implement text data file reading.

8-1) Edit Main Method.

package textfiles;

import java.io.IOException;

public class FileData {

    public static void main(String[] args) throws IOException {

        String file_name = "C:/test.txt";

        ReadFile file = new ReadFile(file_name);

        String[] aryLines = file.OpenFile();

        int i;

        for (i = 0; i < aryLines.length; i++) {

            System.out.println(aryLines[ i]);

        }

    }

}

8-2) Create text file test.txt at file root.

This is line one.

This is line two.

This is line three.

This is line four.

8-3) Run your program.

8-4) Change line limit from 3 to actual line.

Refer Step 7-7 statement “int numberOfLines = 3;

Change to “int numberOfLines = readLines();

8-5) Run your program.

9) Add TRY … CATCH block.

9-1) Edit Main Method.

package textfiles;

import java.io.IOException;

public class FileData {

    public static void main(String[] args) throws IOException {

        String file_name = "C:/test.txt";

        try {

            ReadFile file = new ReadFile(file_name);

            String[] aryLines = file.OpenFile();

            int i;

            for (i = 0; i < aryLines.length; i++) {

                System.out.println(aryLines[ i]);

            }

        } catch (IOException e) {

            System.out.println(e.getMessage());

        }

    }

}

9-2) Test.

Rename the text file to test1.txt.

Run your program again.

REFERENCE

http://www.homeandlearn.co.uk/java/read_a_textfile_in_java.html