This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Convert OutputStream data to String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Convert OutputStream data to String" Watch "Convert OutputStream data to String" New topic
Author

Convert OutputStream data to String

Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
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


David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
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 ]

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
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;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
This old article about altering Streams might help.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
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


Joanne
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Oct 17, 2006
Posts: 212
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

Joined: Sep 26, 2003
Posts: 43
thanks everybody
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Convert OutputStream data to String
 
Similar Threads
Exception in thread "main" java.lang.NoClassDefFoundErro
No unsigned data
calculate the amount values stored form recordstore in J2ME midlet
Exception test
Socket Server Serving Numbers