Text file into Java List using Commons or Guava

前端 未结 7 619
灰色年华
灰色年华 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

    They are pretty similar, with Commons IO it will look like this:

    List lines = FileUtils.readLines(new File("file.txt"), "UTF-8");
    

    Main advantage of Guava is the specification of the charset (no typos):

     List lines = Files.readLines(new File("file.txt"), Charsets.UTF_8);
    

提交回复
热议问题