The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Output of java program

Posted by Kishori Sharan on May 28, 2001 at 4:19 PM

I think your problem is when you run your program your DOS window appears and disappears quickly and you are not able to view your output. You can get your output in two ways :
1) In your program after printing everything just add these lines of code.
try {
System.in.read() ;
}
catch ( Exception e ) {
}

This will wait for you to press a key and then screen will disappear. So, run your appliation with these lines at the end of your program and then press a key when you are done with viewing your out put.
2) Second method is you can redirect your output of System.out.println ( ) method to a file. Suppose, you want to redirect the output to a file named c:\genie\javaoutput.txt then you will have to add the following lines of code in the beginning of your program:
try {
PrintStream fout = new PrintStream ( new FileOutputStream ( "c:\\genie\\javaoutput.txt " ) ) ;
System.setOut ( fout ) ;

}
catch ( Exception e ) {
}

Since you are using printStream class you need to import java.io.* ; in your program. Now all your output will be sent to c:\genie\javaoutput.txt file. Run your application and then open that file to view your output.
A complete example is as follows.

/////////////// Test.java
class Test {

public static void main ( String[] args ) {
int i = 10 ;
byte b = 55 ;


try {
PrintStream fout = new PrintStream ( new FileOutputStream ( "c:\\kishori\\output.txt" ) ) ;
System.setOut ( fout ) ;
//System.in.read() ;
}
catch ( Exception e ) {
}

System.out.println ( "Hello" ) ;

}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us