How come that System.out.println("Hello World") prints to console?
out is a static variable of type PrintStream. On out you call println(). How come it prints to console? Is that a default channel? Could I also say print to a file ?
How come that System.out.println("Hello World") prints to console?
out is a static variable of type PrintStream. On out you call println(). How come it prints to console? Is that a default channel? Could I also say print to a file ?
Yes. The console is the default output stream. In order to write to file using the system.out.println() command you need to change the output stream.
PrintStream fileOut = new PrintStream("./out.txt");
System.setOut(fileOut);
System.out.println("this write in file");
out is instantiated during startup and gets mapped to the standard output console of the host (which is the console by default).
You can change the out object, e.g. instead of standard output to write to a file, using the setOut method:
System.setOut(new PrintStream(new FileOutputStream("log.txt")));
System.out.println("Writing to log.txt");