Today ma biggest task was to digg into redirecting/reassigning the standard output stream to a file(text file). In our case we actually wanted stop anything and everything from being sent to the standard output or the console (command prompt [in windows!!]) as usual coz later we want to check all those output against an expected output file and look for any changes.
Initially I thought it’s going to be a tough task and will take hours. But fortunately it was not the case, It was fairly simple! here I share a simple code to exploit it. I’m sorry the code is not that tidy.
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Lonely {
private File fl = null;
public Lonely() {
try {
fl = new File("stdOut.txt");
fl.canWrite();
System.setOut(new PrintStream(fl));
System.out.println("Writing..");
} catch (Exception e) {
e.printStackTrace();
}
}
private void printOut() {
System.out.println("Hi everyone");
}
public static void main(String[] args) {
Lonely l = new Lonely();
l.printOut();
System.out.println("have a nice day!");
}
}



