Java Files read all lines from text file using US-ASCII charset

Introduction

The strings returned from the readAllLines() method does not include the end of line character.

The readAllLines() method recognizes the following line terminators:

  • \u000D followed by \u000A (CR/LF)
  • \u000A, (LF)
  • \u000D, (CR)
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) {
    Charset cs = Charset.forName("US-ASCII");
    Path source = Paths.get("Main.java");

    try {/*from w  w w. j  a  va 2  s  .c  o m*/
      List<String> lines = Files.readAllLines(source, cs);

      for (String line : lines) {
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related