Feb
07

read a file into List

There are many ways to read file into a List in Java.

Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

commons-io

List<String> lines = FileUtils.readLines(new File("input.txt"), "utf-8");

guava

List<String> lines =  Files.readLines(new File("input.txt"), Charset.forName("utf-8"));

Java 7.0

List<String> list = Files.readAllLines(new File("input.txt").toPath(), Charset.defaultCharset() );

Written by connygy. Posted in java

Tags:

Trackback from your site.

Leave a comment