home

I/O

Reading Contents of a Text File (Java 8)

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

class App {
    public static void main(String[] args) {
        try (Stream<String> lines = Files.lines(Paths.get("/Users/kt/foo.txt"), StandardCharsets.UTF_8)) {
            lines.forEach(System.out::println);
        } catch (IOException e) {
            // Handle it..
        }
    }
}