Here you can find the source of copyStreamToFileBinary(final InputStream stream, final File output)
Parameter | Description |
---|---|
stream | The stream to be consumed. |
output | The file to output to |
Parameter | Description |
---|---|
IOException | an exception |
public static File copyStreamToFileBinary(final InputStream stream, final File output) throws IOException
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { /**// ww w . j a v a 2 s . c om * Helper function for writing a binary stream to a file. The stream and * the file are both closed on completion. * * @param stream The stream to be consumed. * @param output The file to output to * @return The output file * @throws IOException */ public static File copyStreamToFileBinary(final InputStream stream, final File output) throws IOException { final FileOutputStream out = new FileOutputStream(output); final byte buf[] = new byte[1024]; int len = 0; while ((len = stream.read(buf)) > 0) out.write(buf, 0, len); out.close(); stream.close(); return output; } /** * Utility method for quickly create a {@link BufferedReader} for * a given file. * @param file The file * @return the corresponding reader * @throws IOException if an error occurs */ public static BufferedReader read(final File file) throws IOException { return new BufferedReader(new InputStreamReader(new FileInputStream(file))); } }