Computing.Net > Forums > Programming > Read from standard output in java?

Read from standard output in java?

Reply to Message Icon

Original Message
Name: G Chen
Date: July 14, 2004 at 17:53:13 Pacific
Subject: Read from standard output in java?
OS: win2k
CPU/Ram: 1.8g/256m
Comment:

Folks:

For example, HelloWorld.java prints out "Hello World" to standard output (screen display) . How can I write another java program to capture the output "Hello World" after HelloWorld.java is executed? I just don't want to use a temp file to store the data.

Can somebody provide me couple lines of code as a hint?

Thanks a lot !!!

gangchen


Report Offensive Message For Removal


Response Number 1
Name: Tugg
Date: July 15, 2004 at 05:56:35 Pacific
Reply: (edit)

Not quite sure what you mean with capture but im gonne assume you want to print Hello World to a file instead of the Screen...

This would look like the following:

import java.io.*;
class HelloWorldOutput
{
public static void main(String args[])
{
//declare output stream
FileOutputStream out;
//Declare PrintStream
PrintStream p;

try
{
//open OutputStream
out = new FileOutputStream("myfile.txt");
//Link PrintStream to out
p = new PrintStream(out);

//capture text
p.println("Hello World");
//Close Stream
p.close();
}
catch(Exception e)
{
System.err.println("File Output Error");
}
}
}


This code should write Hello World into a text file in the same folder where your .class file is located. If you have any questions bout the code above let me know :)

Hope it helped
~TuGG~



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Read from standard output in java?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge