Java FileInputStream Copy copyFile(InputStream in, String destFile)

Here you can find the source of copyFile(InputStream in, String destFile)

Description

Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE

License

Apache License

Parameter

Parameter Description
in Source data stream
destFile Destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(InputStream in, String destFile) throws IOException 

Method Source Code

//package com.java2s;
/**/*from www  .  ja  v a  2s . c om*/
 * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
     *
     * @param in       Source data stream
     * @param destFile Destination file
     * @throws IOException
     */
    public static void copyFile(InputStream in, String destFile) throws IOException {
        copyFile(in, destFile, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
     *
     * @param sourceFile Source file
     * @param destFile   Destination file
     * @throws IOException
     */
    public static void copyFile(String sourceFile, String destFile) throws IOException {
        copyFile(sourceFile, destFile, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Copy a file to another file using a buffer of maximum size
     *
     * @param sourceFile     Source file
     * @param destFile       Destination file
     * @param copyBufferSize Maximum size of the buffer
     * @throws IOException
     */
    public static void copyFile(String sourceFile, String destFile, int copyBufferSize) throws IOException {
        // Abro los ficheros
        FileInputStream in = new FileInputStream(sourceFile);

        copyFile(in, destFile, copyBufferSize);
    }

    /**
     * Copy a file to another file using a buffer of maximum size
     *
     * @param in             Source data stream
     * @param destFile       Destination file
     * @param copyBufferSize Maximum size of the buffer
     * @throws IOException
     */
    public static void copyFile(InputStream in, String destFile, int copyBufferSize) throws IOException {
        // Abro los ficheros
        FileOutputStream out = new FileOutputStream(destFile);

        try {
            // Transpaso de informacion
            byte[] data = new byte[copyBufferSize];
            int readSize;

            while ((readSize = in.read(data)) != -1)
                out.write(data, 0, readSize);
        } finally {
            // Cierro los ficheros
            in.close();
            out.close();
        }
    }
}

Related

  1. copyFile(final String sourceFile, final String destinationFile)
  2. copyFile(final String sourceFilePath, final String destFilePath)
  3. copyFile(final String sSource, final String sDest)
  4. copyFile(InputStream in, File dst)
  5. copyFile(InputStream in, File to)
  6. copyFile(OutputStream out, InputStream in)
  7. copyFile(String f1, String f2)
  8. copyFile(String fileIn, String fileOut)
  9. copyFile(String fileInName, String fileOutName)