Redirecting System.out to a file

Instantiate a PrintStream that streams to your file and use the System.setOut method.
The same can be done for the input and error streams (setIn and setErr).

import java.io.*;
 
public class Main
{
   public static void main(String args[]) {
      try {
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("log"));
         PrintStream ps = new PrintStream(bos);
         System.setOut(ps);
 
         System.out.println("Hello world!");
 
         ps.close();
      }
      catch(FileNotFoundException e) {
         System.out.println(e);
      }
   }
}