Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

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

$
0
0

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 Files.lines() And Files.newBufferedReader() methods.

Reading file using Files.readAllLines

readAllLines(Path) method will read 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!


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 -

>>>Go to Java Programs Page


Viewing all articles
Browse latest Browse all 938

Trending Articles