Reading Text File Using Java

0
41

This tutorial will teach you about Reading Text File Using Java. Text is a type of file contains a .txt extension.

This tutorial uses BufferedWriterFileNotFoundExceptionIOException, FileReader, and Scanner Libraries.  When the program performs reading, the program read the file from your Netbeans project.  Netbeans project usually located in Documents. Please follow all the steps to complete this tutorial.

Reading a Text file in java Steps

  1. Create a new Java Class and name it what you want.
How Print Hello World Text in Java, Sum Two Integers Values using Java
How Print Hello World Text in Java

2. Insert the following import above your Class.

  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;

3. Inside your main method, declare the following variables. Declare a scanner, string, and Buffer Reader variables.

  1. Scanner input = new Scanner(System.in);
  2. String fileName;
  3. String line = null;
  4. BufferedReader bufferedReader = null;

4. Insert the following statement for user input

  1. System.out.print("Enter file name to read: ");
  2. fileName = input.nextLine();

5. After step 4, insert the following codes below for reading the file.

  1. try{
  2. FileReader fileReader = new FileReader(fileName + ".txt");
  3. bufferedReader = new BufferedReader(fileReader);
  4. while ((line = bufferedReader.readLine()) != null){
  5. System.out.println(line);
  6. }
  7. System.out.println("Unable to open the file!");
  8. }catch(IOException ex){
  9. System.out.println("Error reading file!");
  10. }finally{
  11. try{
  12. if (bufferedReader != null){
  13. bufferedReader.close();
  14. }
  15. }catch(IOException e){
  16. }
  17. }

6. The output should look like the image below.

7. Complete Source Code

  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class FileReading {
  7. public static void main(String args[]) throws IOException{
  8. Scanner input = new Scanner(System.in);
  9. String fileName;
  10. String line = null;
  11. BufferedReader bufferedReader = null;
  12.  
  13. System.out.print("Enter file name to read: ");
  14. fileName = input.nextLine();
  15.  
  16. try{
  17. FileReader fileReader = new FileReader(fileName + ".txt");
  18. bufferedReader = new BufferedReader(fileReader);
  19. while ((line = bufferedReader.readLine()) != null){
  20. System.out.println(line);
  21. }
  22. System.out.println("Unable to open the file!");
  23. }catch(IOException ex){
  24. System.out.println("Error reading file!");
  25. }finally{
  26. try{
  27. if (bufferedReader != null){
  28. bufferedReader.close();
  29. }
  30. }catch(IOException e){
  31. }
  32. }
  33. }
  34. }

If you have question or suggestion about Reading Text File Using Java progam, please feel free to contaact us at our Contact Page.

Facebook Comments
(Visited 42 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here