Java FileInputStream Copy copyFile(File sourceFile, File destinationFile)

Here you can find the source of copyFile(File sourceFile, File destinationFile)

Description

A method that creates a copy of file at new file path

License

Apache License

Parameter

Parameter Description
sourceFile Source file
destinationFile Destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File sourceFile, File destinationFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   ww w .ja  v a 2 s.  c  o m*/
 * Copyright 2009 the original author or authors.
 * Copyright 2009 SorcerSoft.org.
 *  
 * 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.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

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

public class Main {
    /**
     * A method that creates a copy of file at new file path
     * @param sourceFile Source file 
     * @param destinationFile Destination file 
     * @throws IOException
     * @see redirectInputStream2File
     */
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        // If the destination file's parent directory does not exit, create it
        destinationFile.getParentFile().mkdirs();

        //  Creating a new input file stream that is connected to the source file
        InputStream is = new FileInputStream(sourceFile);
        try {
            // Attempting to redirect the input file stream of the source file to the destination file path 
            redirectInputStream2File(is, destinationFile);
        } catch (IOException e) {
            // Caught an IOException 

            // Check if the destination file exist
            if (destinationFile.exists()) {
                // If the destination file exist delete the file 
                destinationFile.delete();
            }
            // The input file stream is reset
            is.reset();
            // Making a second attempt to copy the source file 
            redirectInputStream2File(is, destinationFile);
            // If the redirection fails a second time an IO Exception is thrown
        }
    }

    /**
     * This method redirects an input stream to a file
     * 
     * @param is Input stream
     * @param file File
     * @throws IOException
     */
    public static void redirectInputStream2File(InputStream is, File file) throws IOException {
        int bufferSize = 4096;
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) { // EOF
            fos.write(buffer, 0, bytesRead);
        }
        fos.close();
        is.close();
    }
}

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)
  5. copyFile(File sourceFile, File destinationFile)
  6. copyFile(File sourceFile, File targetDir)
  7. copyFile(File sourceFile, File targetDir)
  8. copyFile(File sourceFile, File targetFile)
  9. copyFile(File sourceFile, File targetFile)