Java PrintStream write String to file

Description

Java PrintStream write String to file

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class Main {
  public static void main(String[] args) {
    String destFile = "Main.java";

    try (PrintStream ps = new PrintStream(destFile)) {
      ps.println("line 1");
      ps.print("line 2");
      ps.println("line 3");
      ps.println("demo from demo2s.com");

      ps.flush();/* w  w w  . ja  v  a 2 s.com*/

      System.out.println("data saved");
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();;
    }
  }
}



PreviousNext

Related