Converting InputStream into File


Web tier: servlets, JSP, Web frameworks: Converting InputStream into File

  1. Converting InputStream into File (2 messages)

     I have a xml file which is stored in a BLOB type in Oracle . Can anybody tell me how can I convert that into a physical file.
    I read the data using inpstream and the question is how to convert or cast it in a physical file.
    Any sample code would be highly appreciated

  2. Converting InputStream into File[ Go to top ]

    There may be a more elegant way to do it, but this should work:

    String fileName = "...";

    InputStream xml = /* from BLOB */;
    BufferedInputStream bis = new BufferendInputStream(xml);

    FileOutputStream fos = new FileOutputStream(fileName);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    int bufferSize = 1024; // 1KB buffer
    int totalRead = 0;
    int read = 0;
    byte[] b = new byte[bufferSize];
    while ((read =, 0, bufferSize)) > 0) {
      bos.write(b, totalRead, read);
      totalRead += read;

    bos.close(); // should flush the stream and close fos also

    Of course, you're gonna have to handle all of the exceptions that can be thrown in the above code, but it's a starting point. Hope this helps.

  3. Converting InputStream into File[ Go to top ]

    Hi Andy
      Thanks a lot. It works. Do u think using PrintStream would be better, because this way it adds some junk words in the beginning of XML file.