It's not a secret anymore!*
The moose likes Beginning Java and the fly likes how to redirect standard in, output, err into a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to redirect standard in, output, err into a file?" Watch "how to redirect standard in, output, err into a file?" New topic
Author

how to redirect standard in, output, err into a file?

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Hi all,
I need to know how to redirect System.out.println("blah..")
into a file?
Waiting for you help?
Mindy
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
public static void setOut(PrintStream´┐Żout)
in class System. Refer to the API-doc.
Hope it helps
Detlev
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Here is the class i created for redirect println to a specific file, my question is how to redirec whatever print out in the System.out.println(...) and System.err.println(...) into a different file? And How come it looks weir when i have special character like a new line (\n)? Can i write any type of object into printstream file?

import java.io.*;

public class RedirectFile
{
FileOutputStream out = null;
FileOutputStream in = null;
FileOutputStream err = null;
PrintStream outFile = null;
PrintStream inFile = null;
PrintStream errFile = null;

public RedirectFile()
{
try
{
out = new FileOutputStream("c:\\temp\\mindy\\out.txt");
in = new FileOutputStream("c:\\temp\\mindy\\in.txt");
err = new FileOutputStream("c:\\temp\\mindy\\err.txt");
outFile = new PrintStream(out);
inFile = new PrintStream(in);
errFile = new PrintStream(err);
outFile.println("this is a testing page");
inFile.println("this is a testing page call inFile....\n");
errFile.println("print the error file...\n");
System.err.println("oops........error");
System.out.println("yeah..out put is ...here ");
}
catch(IOException eIo)
{
System.out.println("can not create file");
}

}
public static void main(String[] args)
{
new RedirectFile();
System.exit(0);
}

}
[This message has been edited by Mindy Wu (edited July 20, 2001).]
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Cindy, how come no one else responses to my question???
I think i figured out how to do it now, for your curiousity, you can add these two lines of code
System.setOut(errFile);
System.setErr(outFile);
before any System.out.println(..) and system.err.println(..) were used. So, all of the output and error message will go to a separate file.
However, i am still not sure how to fix some sepcial character.
Can anyone give me some hints?
Waiting for your help!
Mindy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to redirect standard in, output, err into a file?
 
Similar Threads
Eclipse / MyEclipse - Logging console output ?
Can request.getRequestDispatcher call a servlet?
Log changing
How do u print the error to a file
Redirect output to a file