Close

Java IO & NIO - Files.readAllLines() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Methods:

public static List<String> readAllLines(Path path)
                                 throws IOException
public static List<String> readAllLines(Path path,
                                        Charset cs)
                                 throws IOException

Read all lines from a file.

Parameters:
path - the path to the file
cs - the charset to use for decoding
Returns:
the lines from the file as a List ; whether the List is modifiable or not is implementation dependent and therefore not specified

Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class ReadAllLinesExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");
//write
Files.write(path, "line 1\nline 2\n".getBytes());
//read all lines
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
}
}

Output

line 1
line 2




See Also