Pages

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:

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

    ReplyDelete
  2. 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.

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

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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?

    ReplyDelete
  8. 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.

    ReplyDelete
  9. Just to add my thanks - very useful!

    ReplyDelete
  10. // 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();
    }

    ReplyDelete
  11. 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());

    ReplyDelete