tag:blogger.com,1999:blog-36773632.post116276228123164496..comments2023-12-19T08:40:05.062-05:00Comments on Java Evangelist John Yeary: How to convert an InputStream to a String and BackJohn Yearyhttp://www.blogger.com/profile/00461192445071361043noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-36773632.post-76176524255104587752011-01-14T17:11:09.522-05:002011-01-14T17:11:09.522-05:00Here is a longer example, but I think it is safer ...Here is a longer example, but I think it is safer than using char conversions.<br /><br /> String s = "Hello World!";<br /> byte[] buf = s.getBytes();<br /> ByteArrayInputStream bais = new ByteArrayInputStream(buf);<br /> InputStreamReader isr = new InputStreamReader(bais);<br /> BufferedReader br = new BufferedReader(isr);<br /> CharBuffer cb = CharBuffer.allocate(1024);<br /> br.read(cb);<br /> cb.flip();<br /> System.out.println(cb.toString());John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-70082798563386392822011-01-14T13:49:29.473-05:002011-01-14T13:49:29.473-05:00// A much easier approach
InputStream iFile = /* ...// A much easier approach<br /><br />InputStream iFile = /* get your InputStream here */<br /><br />String s = "";<br />try{<br /> while(true){<br /> int x = iFile.read();<br /> // returns -1 at the end of the stream<br /> if(x == -1)<br /> break;<br /> s = s + (char)x;<br /> }<br />} catch(Exception e){<br /> e.printStackTrace();<br />}Unknownhttps://www.blogger.com/profile/00804591841138550304noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-75613638692599307492009-01-18T18:07:00.000-05:002009-01-18T18:07:00.000-05:00Just to add my thanks - very useful!Just to add my thanks - very useful!Annahttps://www.blogger.com/profile/11127751810696875104noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-82706631475784902692009-01-09T09:49:00.000-05:002009-01-09T09:49:00.000-05:00I imagine the issue has to do with the fact that t...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.John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-18754414456469749422009-01-09T00:47:00.000-05:002009-01-09T00:47:00.000-05:00Hi, My code is this:File file = new File ( "C:/Fil...Hi, My code is this:<BR/>File file = new File ( "C:/File.txt" ) ; <BR/>boolean isFile = file.exists () ;<BR/>InputStream is = new FileInputStream(file) ;<BR/>String s = parseISToString(is); <BR/>System.out.println("Hi :" + s);<BR/><BR/>System.out.println("Hi :"+parseISToString(parseStringToIS(s))); <BR/><BR/>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.<BR/>But my customer says he sees weird characters on screen when he includes "Kanji" chinese characters in comments.<BR/>The outputs of the above two system outs are different when the text file has chinese characters. What does that mean?Bajalhttps://www.blogger.com/profile/04329816256239799124noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-85103683188175037142008-10-16T12:20:00.000-05:002008-10-16T12:20:00.000-05:00Thanks for the help!Thanks for the help!Craig Sumnerhttps://www.blogger.com/profile/01474964871687610187noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-33574851249311651462008-10-05T18:10:00.000-05:002008-10-05T18:10:00.000-05:00Although, you may find the API "ugly", it still wi...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.John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-19212015410424524822008-10-04T17:42:00.000-05:002008-10-04T17:42:00.000-05:00The input stream => string conversion code isn&...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.<BR/><BR/>Fundamentally, StringReader.readLine() is an ugly API.Neilhttps://www.blogger.com/profile/02292645236366550571noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-74964965723712015442008-08-17T16:48:00.000-05:002008-08-17T16:48:00.000-05:00I am glad that worked for you. It is validation th...I am glad that worked for you. It is validation that the code posted does work.John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-36705920515467767332008-08-17T01:07:00.000-05:002008-08-17T01:07:00.000-05:00Just a word of Thanks!This works perfect for me. Y...Just a word of Thanks!<BR/>This works perfect for me. You saved me at least 3 hours :)Bajalhttps://www.blogger.com/profile/04329816256239799124noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-26826524839634600772007-07-16T20:48:00.000-05:002007-07-16T20:48:00.000-05:00I can not guess at what you may have encountered. ...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.John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-36201706232828070442007-06-07T09:06:00.000-05:002007-06-07T09:06:00.000-05:00Well, it isn't working...I am using sockets and is...Well, it isn't working...I am using sockets and is I try to use this I get a Connection CLose exception.Unknownhttps://www.blogger.com/profile/04366852189060623607noreply@blogger.com