Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 19
    Picking a Reader really depends on what you need the content of the file for. If the file is small(ish) and you need it all, it's faster (benchmarked by us: 1.8-2x) to just use a FileReader and read everything (or at least large enough chunks). If you're processing it line by line then go for the BufferedReader. Commented Aug 27, 2013 at 13:45
  • 4
    Will the line order be preserved when using "Files.lines(..).forEach(...)". My understanding is that the order will be arbitrary after this operation. Commented Sep 14, 2014 at 18:49
  • 50
    Files.lines(…).forEach(…) does not preserve order of lines but is executed in parallel, @Dash. If the order is important, you can use Files.lines(…).forEachOrdered(…), which should preserve the order (did not verify though). Commented Feb 15, 2015 at 22:45
  • 2
    @Palec this is interesting, but can you quote from the docs where it says that Files.lines(...).forEach(...) is executed in parallel? I thought this was only the case when you explicitly make the stream parallel using Files.lines(...).parallel().forEach(...). Commented Nov 9, 2015 at 14:03
  • 5
    My original formulation is not bulletproof, @KlitosKyriacou. The point is that forEach does not guarantee any order and the reason is easy parallelization. If order is to be preserved, use forEachOrdered. Commented Nov 10, 2015 at 5:48