Java BufferedInputStream Copy copyFileFromStream(InputStream in, File dest)

Here you can find the source of copyFileFromStream(InputStream in, File dest)

Description

copy File From Stream

License

Open Source License

Declaration

public static void copyFileFromStream(InputStream in, File dest) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source 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 {
    public static void copyFileFromStream(InputStream in, File dest) throws IOException {

        try {//from www.  j  a v a 2  s.c o m
            if (!dest.exists()) {
                dest.getParentFile().mkdirs();
                dest.createNewFile();
            }
        } catch (IOException e) {
            throw e;
        }

        OutputStream out = null;
        BufferedInputStream bin = new BufferedInputStream(in);
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        try {
            out = new FileOutputStream(dest);
        } catch (FileNotFoundException e1) {
            throw new RuntimeException("Impossible! File not found?!", e1);
        }

        try {
            while ((bytesRead = bin.read(buffer)) > 0) {

                out.write(buffer, 0, bytesRead);

            }
        } catch (IOException e) {
            throw e;
        } finally {

            try {
                bin.close();
            } catch (IOException e) {
                throw new RuntimeException("Cannot close input stream!", e);
            }

            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("Cannot close output stream!", e);
            }

        }

    }
}

Related

  1. copyFile(String source, String destination)
  2. copyFile(String source, String destination)
  3. copyFile(String sourceFilePath, String destinationFilePath)
  4. copyFile(String src, String dest)
  5. copyFileBytes(String srcFileName, String tarFileName)
  6. copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)
  7. copyFileNormal(File copyFrom, File copyTo)
  8. copyFileToFile(File in, File out)
  9. copyFileToStream(File from, OutputStream out)