How to read a file in Java?

How to open and read a text file in Java
  • you can use java.io.BufferedReader to read content from a file.

how to read a file in Java with java.io.BufferedReader

First Example:

package com.nourit.javaIO;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadFileWithBufferedReader {

  public static void main(String[] args) throws IOException {
    String line;
    FileInputStream fileInputStrem = null;
    BufferedReader bufferedReader = null;
    InputStreamReader inputStreamReader = null;

    try {
      fileInputStrem = new FileInputStream("D:/tmp/nourit.txt");
      inputStreamReader = new InputStreamReader(fileInputStrem);
      bufferedReader = new BufferedReader(inputStreamReader);
      while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
      }
    } finally {
      if (bufferedReader != null){
        bufferedReader.close();
      }
      if (inputStreamReader != null){
        inputStreamReader.close();
      }
      if (fileInputStrem != null){
        fileInputStrem.close();
      }
    }
  }

}

Output:

nour-it Test line 1
nour-it Test line 2
nour-it Test line 3

Second Example:

package com.nourit.javaIO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileWithBufferedReader2 {

  public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = null;
    try {

      bufferedReader = new BufferedReader(new FileReader("D:/tmp/nourit.txt"));

      String sCurrentLine;

      while ((sCurrentLine = bufferedReader.readLine()) != null) {
        System.out.println(sCurrentLine);
      }

    } finally {
      if (bufferedReader != null){
        bufferedReader.close();
      }
    }
  }
}

Output:

nour-it Test line 1
nour-it Test line 2
nour-it Test line 3

Third Example: You don’t need to close anything because readAllLines() is taking care of that.

package com.nourit.javaIO;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class ReadFileWithBufferedReader3 {

  public static void main(String[] args) throws IOException {
     Path path = Paths.get( "D:/tmp/nourit.txt" );
        List <String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        for (String element : lines) {
           System.out.println(element);
         }
  }

}

Output:

nour-it Test line 1
nour-it Test line 2
nour-it Test line 3

 

 

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Ein Gedanke zu „How to read a file in Java?

  • 27. September 2018 um 20:04
    Permalink

    Excellent programming examples. thnx for giving wonderful programs.

    Antwort

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*