Here you can find the source of readToFile(InputStream in, File localFile)
Parameter | Description |
---|---|
in | The InputStream |
localFile | The local file |
Parameter | Description |
---|---|
IOException | If reading or writing fails |
private static void readToFile(InputStream in, File localFile) throws IOException, FileNotFoundException
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**//from w w w . java 2s .c o m * Reads the whole InputStream content into a local file * * @param in * The InputStream * @param localFile * The local file * @throws IOException * If reading or writing fails */ private static void readToFile(InputStream in, File localFile) throws IOException, FileNotFoundException { OutputStream output = null; try { output = new FileOutputStream(localFile); // download the file in = new BufferedInputStream(in); byte data[] = new byte[1024]; int count; while ((count = in.read(data)) != -1) { output.write(data, 0, count); } output.flush(); output.close(); } finally { if (output != null) { output.flush(); // close the local file in any case output.close(); } } } }