PrintStream fileOut = new PrintStream(new FileOutputStream("output.txt")); System.setOut(fileOut); System.out.println("This text will be written to a file instead of the console.");
PrintStream otherOut = new PrintStream(new Socket("localhost", 1234).getOutputStream()); System.setOut(otherOut); System.out.println("This text will be sent to another output stream.");The setOut() method belongs to the java.lang.System class, which is part of the Java Standard Library.