• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

large output and error messages

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
i am new to Java ranch!
How do u get to have large outputs and error messages to be directed into a file rather than the DOS Window
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish,
You can create a new PrintStream that is chained to a FileOutputStream that points to your output file. You then reassign the standard System.out PrintStream to your new PrintStream using System.setOut.
I have included a sample class below that reassigns System.out to print to a file and then reassigns it back to print to the standard terminal output:
import java.io.*;
public class Reassign {
public static void main( String args[] ) {
try {
// Optional capture of original standard out print
// stream to reset to default later if desired
PrintStream ops = System.out;
// Create new file and print steams for new standard out
FileOutputStream fs = new FileOutputStream("Test.dat");
PrintStream nps = new PrintStream(fs);
// Reassign standard out to new print stream
System.setOut(nps);
System.out.println( "Reassign to new PrintStream" );
nps.close();
// Reassign standard out to old default print stream
System.setOut(ops);
System.out.println( "Reassign to old PrintStream" );
ops.close();
}
catch (IOException e ) {
System.out.println( "Standard out could not be"
+ " reassigned");
}
}
}
Hope this helps,
Bob Kerfoot - SCJP
 
Manish Vig
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx
I think this would certainly help,
I twas looking for some option at commandline that could do the stuff,There is none for javac but i thought there is another way.
Anyway thanks
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish,
Understood. In the case of NT you can normally redirect your output via the command line as follows:
"java pgm > filename.txt"
However, with "javac pgm.java > filename.txt" the output still goes to the screen and the filename.txt file ends up empty. Maybe someone else can provide another solution.
Bob Kerfoot - SCJP
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic