OneCompiler

Read text file lines in to List<String> in Java

280

Utility method for reading text file's lines into a List<String> in Java

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

public static List<String> lines(Path path) {
    try {
        return Files.readAllLines(path)
    } catch (IOException e) {
        throw new RuntimeException("exception while reading lines from file: " + path)
    }
}