Java BufferedReader read text file line by line and restore line separator

Description

Java BufferedReader read text file line by line and restore line separator


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Main {
  public static void main(String[] args) throws Exception {
    File file = new File("Main.java");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    reader = new BufferedReader(new FileReader(file));
    String text = null;/*www. j a  v a 2s  .co  m*/

    // repeat until all lines is read
    while ((text = reader.readLine()) != null) {
      contents.append(text).append(System.getProperty("line.separator"));
    }
    reader.close();
    System.out.println(contents.toString());
  }
}



PreviousNext

Related