Java I/O How to - Turn System.out into a PrintWriter








Question

We would like to know how to turn System.out into a PrintWriter.

Answer

   
import java.io.PrintWriter;

public class Main {
  public static void main(String[] args) {
    PrintWriter out = new PrintWriter(System.out, true);
    out.println("Hello, world");
  }
}

The code above generates the following result.