Both two method will return stream of data, Is there any different between these two methods? If it's Which way is more suitable to read the large files?
Asked
Active
Viewed 820 times
2
-
Why do you think there is a difference? – Louis Wasserman Jun 15 '21 at 13:13
-
4In Java 9, there is an [implementation note](https://docs.oracle.com/javase/9/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-java.nio.charset.Charset-) in the docs of `Files.lines` that says it offers more parallelism if the charset is _line-optimal_, and if you check the source code, you see that it checks for the charset, and if it's not one of the line-optimal ones, it falls back to creating a buffered reader and calling `lines` on that, which implies that `newBufferedReader().lines` doesn't have this optimisation. Don't know about Java 8 though. It doesn't have that note. – Sweeper Jun 15 '21 at 13:19
-
[At least for OpenJDK](https://github.com/AdoptOpenJDK/openjdk-jdk8u/blob/master/jdk/src/share/classes/java/nio/file/Files.java#L3744), in Java 8 they are the same, but as the answer says, do remember to close the buffered reader. – Sweeper Jun 15 '21 at 13:21
-
Yeah just download the source code for your JDK, and link it in your IDE, so that you can always immediately check out code things like this. Quite often this helps wonders with problems, and other times yuo might learn something new. – JayC667 Jun 15 '21 at 13:32
-
@JayC667 I agree and do so frequently. But write the code so the code works with the publicized behavior. Otherwise, it may fail in the future due to changes in the internal implementation. – WJS Jun 15 '21 at 15:38
-
1@Sweeper yes, that optimization has been added in Java 9 – Holger Jun 16 '21 at 06:55
-
1https://bugs.openjdk.java.net/browse/JDK-8072773 – Holger Jun 16 '21 at 07:03
1 Answers
4
The difference is, Files.lines
gives you a BaseStream
which you have to close to prevent resource leakage. Files.newBufferedReader
on the other hand gives you a Reader
which you have to close. So in the end Files.lines
is a shortcut if you are only interested in the lines as a Stream
. Otherwise it behaves pretty similar:
Path path=Paths.get("file.txt");
try(Stream<String> stream=Files.lines(path))
{
stream.forEach(System.out::println);
}
try(BufferedReader reader=Files.newBufferedReader(path))
{
reader.lines().forEach(System.out::println);
}
As the Java 9 Javadoc for Files.lines
states in the "Implementation Note", it is optimized for parallelization for the StandardCharset
s UTF-8
, US-ASCII
and ISO-8859-1
. And thus to prefer for larger files with one of those encodings.

Sascha
- 1,320
- 10
- 16
-
1@Holger The javadoc for the `lines(Path)` method doesn't say that, so it escaped my notice, but the referred `lines(Path,Charset)` does. I will edit my answer to address it property. – Sascha Jun 16 '21 at 07:30
-
1The behavioral difference regarding resource cleanup surely is the most important one. But since the OP asked “Which way is more suitable to read the large files?”, it’s worth mentioning the optimization added in Java 9 which is only present in `Files.lines` as mentioned in [this comment](https://stackoverflow.com/questions/67986730/67987026?noredirect=1#comment120166616_67986730) and [2](https://stackoverflow.com/questions/67986730/67987026?noredirect=1#comment120185121_67986730) and [3](https://stackoverflow.com/questions/67986730/67987026?noredirect=1#comment120185331_67986730). – Holger Jun 16 '21 at 08:16
-
@Holger I will work that in. (This becomes so much more complex by every iteration.) – Sascha Jun 16 '21 at 11:03