Here you can find the source of copyStreamToFile(InputStream is, File outputFile)
Parameter | Description |
---|---|
is | input stream |
outputFile | target file |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyStreamToFile(InputStream is, File outputFile) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**//from w w w . j a v a 2s. c o m * Copy contents of a stream to a target file. Leave the input * stream open afterwards. * @param is input stream * @param outputFile target file * @throws IOException */ public static void copyStreamToFile(InputStream is, File outputFile) throws IOException { OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile)); copyStreamToStream(is, os); os.close(); } /** * Copy contents of an input stream to an output stream. Leave both * streams open afterwards. * @param is input stream * @param os output stream * @throws IOException */ public static void copyStreamToStream(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[4096]; for (int read = is.read(buffer); read != -1; read = is.read(buffer)) { os.write(buffer, 0, read); } os.flush(); } }