Text file into Java List using Commons or Guava

前端 未结 7 639
灰色年华
灰色年华 2021-01-08 01:18

What is the most elegant way to put each line of text (from the text file) into LinkedList (as String object) or some other collection, using Commons or Guava libraries.

7条回答
  •  长情又很酷
    2021-01-08 02:08

    I'm not sure if you only want to know how to do this via Guava or Commons IO, but since Java 7 this can be done via java.nio.file.Files.readAllLines(Path path, Charset cs) (javadoc).

    List allLines = Files.readAllLines(dir.toPath(), StandardCharsets.UTF_8);
    

    Since this is part of the Java SE it does not require you to add any additional jar files (Guava or Commons) to your project.

提交回复
热议问题