permaculture playing cards*
The moose likes Java in General and the fly likes writing process output stream (p.getOutputStream) to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "writing process output stream (p.getOutputStream) to a file " Watch "writing process output stream (p.getOutputStream) to a file " New topic
Author

writing process output stream (p.getOutputStream) to a file

Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
Hi
i am calling a java process from my web UI. i am using runtime.exec to execute an external java program. i need to redirect the process output stream to a file. That means all the system.out and system.err should be written to a file. i dont how to redirect the p.getOutputStream() to a file. Here is the code which i am using which does not work
Process p = Runtime.getRuntime().exec("java -cp some program); BufferedOutputStream o = (BufferedOutputStream) p.getOutputStream();
String str = o.toString();
if (str !=null){
byte buf[] = str.getBytes();
OutputStream os = new FileOutputStream("syndierr.txt");
os.write(buf);
os.close();
}
Any help will be appreciated.
Rashid
[ February 04, 2006: Message edited by: Michael Ernest ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Read this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
i need to know how can i direct the process output stream into a file. Do i need to convert the process output stream to an inputStream and then pass it as a source for the File OutputStream to read it into the file. its a bit confusing. i tried using buffer input stream and output stream but then also the file is having contents like. java.io.BufferedOutputStream@228a02
Any examples
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Note that the standard error and standard output streams of your process are separate streams. if you want to merge them, I suggest using ProcessBuilder.


There is no emoticon for what I am feeling!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Rashid Darvesh:
i tried using buffer input stream and output stream but then also the file is having contents like. java.io.BufferedOutputStream@228a02
Any examples

You are writing the stream object to the output file. You need to read the content of the stream and write the content to the output file. Have a look at the Java Tutorial on IO for the basics of working with Java IO.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mr. Ankur Garg
Greenhorn

Joined: May 27, 2012
Posts: 6

hey to read the output of any process you should use
getInputStream of that process to get the output
and getOutputStream serves as an input to process


Ankur Garg (SCJP,SCWCD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing process output stream (p.getOutputStream) to a file
 
Similar Threads
Open the notepad with some text at runtime
Writting confirmation message??
Writting confirmation message??
skip() method of InputStream???
Connection Broken & Check??