Reading text file with the readAllLines() Method - Java File Path IO

Java examples for File Path IO:File Operation

Description

Reading text file with the readAllLines() Method

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Path wiki_path = Paths.get("C:/folder1/wiki", "wiki.txt");
    Charset charset = Charset.forName("ISO-8859-1");
    try {/*from ww w.j  a v a  2s . c o m*/
      List<String> lines = Files.readAllLines(wiki_path, charset);
      for (String line : lines) {
        System.out.println(line);
      }
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}

Result

Files.readAllLines recognizes the following as line terminators:

  • \u000D followed by \u000A: CARRIAGE RETURN followed by LINE FEED
  • \u000A: LINE FEED
  • \u000D: CARRIAGE RETURN

Related Tutorials