import java.io.*; public class RedirectedOutput { public static void main(String[] args) throws Exception { FileOutputStream fout = new FileOutputStream("output.txt"); PrintStream ps = new PrintStream(fout); System.setOut(ps); System.out.println("Hello, this will be redirected to a file"); ps.close(); } }
import java.io.*; public class RedirectedOutput { public static void main(String[] args) throws Exception { System.setOut(new PrintStream(new OutputStream() { public void write(int b) {} })); System.out.println("This output is redirected to null."); } }In this example, java.io package is used to redirect the standard output to null. Package Library: java.io.