• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I need to know how to redirect System.out.println("blah..")
into a file?
Waiting for you help?
Mindy
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void setOut(PrintStream�out)
in class System. Refer to the API-doc.
Hope it helps
Detlev
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic