• 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

Convert OutputStream data to String

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i convert a OutputStream data to String. Since OutputStream can only write, i am trouble in converting it to String. Here is my full java code


 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean how do I write a String with an OutputStream?

Look at the API docs, you can't only byte[] or int. String does have a method that converts the String to byte[]. However, if you read the FileOutputStream docs it tells you this:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

Or you can use ObjectOutputStream and call writeUTF(), but FileWriter is a better option.
[ May 01, 2007: Message edited by: David McCombs ]
 
Amar Naik
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once i get the output Runtime.getRuntime().exec(program), i want to display the output as string.

The "process API" has a method "getOutputStream()" which gives me the output in OutputStream . I want to convert this output to String;
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This old article about altering Streams might help.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Process's OutputStream is what you use to write data to the Process. If you want to see what the Process is outputting, read the InputStream and ErrorStream
 
Marshal
Posts: 28226
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
The getOutputStream() method gets you an OutputStream you can use to write data to the stdin of the Process you created. As such, the idea of converting that to a String doesn't make any sense.

But if your actual goal is to read the data from the stdout of the Process and put it into a String, then the getInputStream() method gets you stdout data. (It is always a good idea to state your actual goal when you post a question, rather than asking how to implement the idea you had to achieve that goal. That way when it turns out your idea is wrong, you have still asked your question.)
[ May 01, 2007: Message edited by: Paul Clapham ]
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I misunderstood your question. But I think you are misunderstanding what getOutputStream() does. It returns an OutputStream object so you can write to whatever it is connected to. It does not give you the output. You can call toString() but it will not give you any useful information.

An OutputStream is for writing to an underlying process, file, stdout, socket, whatever.
 
Amar Naik
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everybody
reply
    Bookmark Topic Watch Topic
  • New Topic