Java InputStream to OutputStream copyStreamToFile(InputStream input, String outputPath)

Here you can find the source of copyStreamToFile(InputStream input, String outputPath)

Description

copy Stream To File

License

LGPL

Declaration

public static File copyStreamToFile(InputStream input, String outputPath) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static File copyStreamToFile(InputStream input, String outputPath) {
        File file = new File(outputPath);
        try {//from  w  w  w.  j  av  a2s.c om
            DataOutputStream out = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(file)));
            // Transfer bytes from in to out
            InputStream is = input;
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            is.close();
            out.close();
        } catch (IOException e) {
            System.err.println("Error Writing/Reading Streams.");
        }
        return file;
    }
}

Related

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