Thursday, 6 December 2018

Reading File in Java Using Files.lines And Files.newBufferedReader

You would have used BufferedReader to read files in Java but Java 8 onward there is another way to read files using methods of Files class (Files class itself was added in Java 7).

In this post we'll see examples of reading file in Java using the following methods which are available Java 8 onward.

  • Using Files.readAllLines() method. See example.
  • Using Files.lines() method and using Files.newBufferedReader() method. See example.

Reading file using Files.readAllLines

readAllLines(Path) method reads all the lines of the file into a list of String. It is not a very efficient way to read a file as a whole file is stored in a list which means consuming more memory.

public class ReadFile {
    public static void main(String[] args) {
        Path path = Paths.get("G:\\Temp.txt");
        
        try {
            List<String> fileList = Files.readAllLines(path);
            System.out.println("" + fileList);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Reading file using Files.lines and Files.newBufferedReader

In Java 8 lines() method has been added in Files class which provides a better way to read files. This method won't read all lines of the file at once but read lines from a file as a Stream line by line.

Another method is newBufferedReader() which returns a BufferedReader to read text from the file in an efficient manner.

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class ReadFile {

    public static void main(String[] args) {
        Path path = Paths.get("G:\\Temp.txt");
        // Using Lines
        try(Stream<String> stream = Files.lines(path)){
            stream.forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        // Using newBufferedReader
        try(BufferedReader br = Files.newBufferedReader(path)){
            Stream<String> stream = br.lines();
            stream.forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Since lines and newBufferedReader methods return Stream so you can also use this functional stream to write a chain of streams doing some extra processing.

As example if file has blank lines and you want to filter those blank lines and display only non-blank lines then it can be written this way in Java.

public class ReadFile {

    public static void main(String[] args) {
        Path path = Paths.get("G:\\Temp.txt");
        
        try(Stream<String> stream = Files.lines(path)){
            // print only if line is not blank
            stream.filter(line->!line.trim().equals(""))
            .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

That's all for this topic Reading File in Java Using Files.lines And Files.newBufferedReader. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Reading File in Java Using Scanner
  2. Writing File in Java
  3. How to Find Last Modified Date of a File in Java
  4. How to Read Properties File in Java
  5. Stream API in Java 8

You may also like -

  1. Zipping Files in Java
  2. Print Odd-Even Numbers Using Threads And wait-notify
  3. Converting int to String - Java Program
  4. Getting All The Tables in a Schema in a DB - Java Program
  5. Callable And Future in Java Concurrency
  6. How HashMap Internally Works in Java
  7. Polymorphism in Java
  8. Method Reference in Java 8

No comments:

Post a Comment