Here you can find the source of dumpStreamToFile(InputStream is, String filename)
Parameter | Description |
---|---|
is | a parameter |
filename | a parameter |
public static void dumpStreamToFile(InputStream is, String filename)
//package com.java2s; //License from project: Academic Free License import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; public class Main { /**/*from w w w.j a va 2s .co m*/ * Dump the input stream to the specified file. * * @param is * @param filename */ public static void dumpStreamToFile(InputStream is, String filename) { FileOutputStream fos = null; try { fos = new FileOutputStream(filename); dumpStreamToStream(is, fos); } catch (Exception ex) { ex.printStackTrace(); } finally { close(fos); } } /** * Dump the input stream to the output stream. * * @param is * @param os */ public static void dumpStreamToStream(InputStream is, OutputStream os) { byte ba[] = new byte[2048]; int numRead = 0; try { numRead = is.read(ba); while (numRead > 0) { os.write(ba, 0, numRead); numRead = is.read(ba); } } catch (Exception ex) { ex.printStackTrace(); } finally { if (os != null) try { os.flush(); } catch (Exception ex) { } ; } } /** * Close the output stream and trap any exceptions. */ public static void close(OutputStream os) { if (os != null) { try { os.close(); } catch (Exception ex) { } } } public static void close(Writer fw) { if (fw != null) { try { fw.close(); } catch (Exception ex) { } ; } } /** * Close the input stream and trap any exceptions. */ public static void close(InputStream is) { if (is != null) { try { is.close(); } catch (Exception ex) { } } } /** * Close the reader. * * @param r */ public static void close(Reader r) { if (r != null) { try { r.close(); } catch (Exception ex) { } } } }