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

Reading file in Java 8

$
0
0

If anybody asks to point out two most prominent new features of Java 8, I guess most of the people will say Lambda expressions (along with Stream API) and interface default methods. These are definitely impactful changes and lambda expressions along with Streams provides a very easy way to code complex tasks. But that's not all Java 8 also provides another way to read files, you would have used BufferedReader to read files in Java but with Java 8 there is a way to read files using methods of Files class (Files class itself was added in Java 7).

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 provide 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.


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 8. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Reading file in Java using BufferedReader
  2. How to read file from the last line in Java
  3. How to find last modified date of a file in Java
  4. Reading file in Java using Scanner
  5. Reading delimited file in Java using Scanner
  6. try-with-resources in Java 7

You may also like -

>>>Go to Java Programs page


Viewing all articles
Browse latest Browse all 862

Trending Articles