Here you can find the source of copyStreamToFile(final InputStream stream, final File output)
Parameter | Description |
---|---|
stream | the stream will be consumed |
output | file |
Parameter | Description |
---|---|
IOException | an exception |
public static File copyStreamToFile(final InputStream stream, final File output) throws IOException
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { /**//from w w w . j a v a 2 s . com * Helper function for writing a text stream to a file. * @param stream the stream will be consumed * @param output file * @return a temporary file with the stream context written * @throws IOException */ public static File copyStreamToFile(final InputStream stream, final File output) throws IOException { final BufferedReader r = new BufferedReader(new InputStreamReader(stream)); String l = null; final PrintWriter writer = new PrintWriter(new FileWriter(output)); while ((l = r.readLine()) != null) { writer.println(l); } writer.close(); r.close(); return output; } }