Code for redirecting the print output within the program:
try
{
System.out.println("Print on console");
// Store console print stream.
PrintStream ps_console = System.out;
File file = new File("C:/Users/John/Desktop/compiletest/output" + trialNum + ".txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println("Print in the file !!");
// Set console print stream.
//System.setOut(ps_console);
System.out.println("Console again !!");
}
catch(Exception e)
{
System.out.println("Mission failed, we'll get em next time");
}
This method works fine if it is implemented in the program I want to redirect its output. In my case I want a separate class that will redirect the output of a certain program to a txt file.
If there is a simpler way to complete this task I am greatly inclined to hear.