Java InputStream to OutputStream copyStreamToFile(final InputStream stream, final File output)

Here you can find the source of copyStreamToFile(final InputStream stream, final File output)

Description

Helper function for writing a text stream to a file.

License

Open Source License

Parameter

Parameter Description
stream the stream will be consumed
output file

Exception

Parameter Description
IOException an exception

Return

a temporary file with the stream context written

Declaration

public static File copyStreamToFile(final InputStream stream, final File output) throws IOException 

Method Source Code

//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;
    }
}

Related

  1. copyStreams(InputStream inputStream, OutputStream outputStream)
  2. copyStreams(InputStream is, OutputStream os)
  3. copyStreams(InputStream source, OutputStream destination)
  4. copyStreams(InputStream source, OutputStream target)
  5. copyStreamSafely(InputStream in, ByteArrayOutputStream os)
  6. copyStreamToFile(InputStream input, String outputPath)
  7. CopyStreamToFile(InputStream inputStream, File outputFile)
  8. copyStreamToFile(InputStream inputStream, File outputFile)
  9. copyStreamToFile(InputStream is, File outputFile)