Converting InputStream into File

Discussions

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

  1. Converting InputStream into File (2 messages)

    Hi
     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

    Thanks
    Asad
  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 = bis.read(b, 0, bufferSize)) > 0) {
      bos.write(b, totalRead, read);
      totalRead += read;
    }

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

    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.

    Andy
  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.

    ThankYou
    Bye
    Asad