Java FileOutputStream Write copyFileUsingStream(InputStream sourceStream, File dest)

Here you can find the source of copyFileUsingStream(InputStream sourceStream, File dest)

Description

Copy file from a given InputStream to a destination file

License

Open Source License

Parameter

Parameter Description
sourceStream source file as an InputStream
dest destination given as a File

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFileUsingStream(InputStream sourceStream, File dest) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w w w.ja  v  a 2 s . co m*/
 *  This file is part of easyFPGA.
 *  Copyright 2013-2015 os-cillation GmbH
 *
 *  easyFPGA is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  easyFPGA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with easyFPGA.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    /**
     * Copy file from a given InputStream to a destination file
     *
     * @param sourceStream source file as an InputStream
     * @param dest destination given as a File
     * @throws IOException
     */
    public static void copyFileUsingStream(InputStream sourceStream, File dest) throws IOException {

        if (sourceStream == null) {
            throw new IllegalArgumentException("Source stream is null");
        }

        OutputStream destStream = null;
        try {
            destStream = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = sourceStream.read(buffer)) > 0) {
                destStream.write(buffer, 0, length);
            }
        } finally {
            sourceStream.close();
            destStream.close();
        }
    }
}

Related

  1. copyFile(InputStream input, File outputLocation)
  2. copyFile(InputStream input, OutputStream output)
  3. copyFile(InputStream sourceFileIs, File destDirFile, String destFileName)
  4. copyFileFromAssets(InputStream inputStream, String pathToWrite)
  5. copyFileUsingFileStreams(InputStream source, File dest)
  6. save(byte[] bs, String fn)
  7. save(byte[] bytes, File path)
  8. save(byte[] data, String path)
  9. save(File file, byte[] bytes)