How do I grab the output sent to the console and save it to a .txt file?

Thanks in advanced.

Redirect System.out Something like...

String fileName = "whatever.txt";
final boolean append = true, autoflush = true;
PrintStream printStream = new PrintStream(new FileOutputStream(fileName, append),
                                    autoflush);
System.setOut(printStream);

I tried that code didn't work. I need all output including errors sent to the .txt file.

Thanks for the support but It doesn't print my "FLOCKA" or my error to the .txt file.

Check out my code

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;



public class test {
	public static void main(String[] args){

		System.out.println("FLOCKA");
		System.err.print("booo");	    
	    	try
	    	{
	    	
	    	

	    	String fileName = "whatever.txt";
	    	final boolean append = true, autoflush = true;
	    	PrintStream printStream = new PrintStream(new FileOutputStream(fileName, append),  autoflush);
	    	System.setOut(printStream);
	    	System.setErr(printStream);
	    	
	      
	    	}
	    	catch(IOException e)
	    	{
	    	System.out.println("Error during reading/writing");
	    	}
	    
	}

}

Edited 5 Years Ago by smoothe19: n/a

It would help if you redirected the output before sending it.
You need to run the redirection code when the app starts, then all subsequent out/err output goes to that file.

Ahahahaha oopsie i feel slow. Thank you JamesCherrill i googled/yahoo/bing search noone knew haha now everyone who searches the same will land on this topic :-D

This question has already been answered. Start a new discussion instead.