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

Thanks in advanced.

Recommended Answers

All 7 Replies

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.

and

System.setErr(printStream);

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");
	    	}
	    
	}

}

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

OK, glad to help.
Mark this thread "solved" so people know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.