• 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

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

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic