Sunday, November 05, 2006

How to convert an InputStream to a String and Back

There are four methods listed below to convert InputStream to String and String to an InputStream.

12 comments :

Unknown said...

Well, it isn't working...I am using sockets and is I try to use this I get a Connection CLose exception.

John Yeary said...

I can not guess at what you may have encountered. This code does work. It would help if you posted a code snippet to show where its broken. Using sockets can cause a number of issues which are not necessarily related to converting an InputStream to a String, or vice-versa.

Bajal said...

Just a word of Thanks!
This works perfect for me. You saved me at least 3 hours :)

John Yeary said...

I am glad that worked for you. It is validation that the code posted does work.

Neil said...

The input stream => string conversion code isn't quite right: if the last line of the file doesn't end with a newline, the resulting String won't accurately represent the contents of the file.

Fundamentally, StringReader.readLine() is an ugly API.

John Yeary said...

Although, you may find the API "ugly", it still will accomplish the job most of the time. There are other methods available, if this does not meet your needs.

Craig Sumner said...

Thanks for the help!

Bajal said...

Hi, My code is this:
File file = new File ( "C:/File.txt" ) ;
boolean isFile = file.exists () ;
InputStream is = new FileInputStream(file) ;
String s = parseISToString(is);
System.out.println("Hi :" + s);

System.out.println("Hi :"+parseISToString(parseStringToIS(s)));

Basically, parseISToString is on client side and parseStringtoIS is on server side. I do this because InputStream is not serializable and cannot be send across EJB layer.
But my customer says he sees weird characters on screen when he includes "Kanji" chinese characters in comments.
The outputs of the above two system outs are different when the text file has chinese characters. What does that mean?

John Yeary said...

I imagine the issue has to do with the fact that the encoding is UTF-8. Since Chinese requires multi-byte to display its glyphs, you will need to use a different encoding. Try using a different encoding like UTF-16.

Anna said...

Just to add my thanks - very useful!

Unknown said...

// A much easier approach

InputStream iFile = /* get your InputStream here */

String s = "";
try{
while(true){
int x = iFile.read();
// returns -1 at the end of the stream
if(x == -1)
break;
s = s + (char)x;
}
} catch(Exception e){
e.printStackTrace();
}

John Yeary said...

Here is a longer example, but I think it is safer than using char conversions.

String s = "Hello World!";
byte[] buf = s.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
InputStreamReader isr = new InputStreamReader(bais);
BufferedReader br = new BufferedReader(isr);
CharBuffer cb = CharBuffer.allocate(1024);
br.read(cb);
cb.flip();
System.out.println(cb.toString());

Popular Posts