Java FileInputStream Copy copyFile(File source, File target)

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

Description

Creates a copy of a file

License

Open Source License

Parameter

Parameter Description
source the source file
target the target file

Return

true if written successfully

Declaration

public static boolean copyFile(File source, File target) 

Method Source Code


//package com.java2s;
/*//from  ww w  .j  a va  2 s  . c  o  m
* Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
* File utility functions
*
*/

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    /**
     * File copy buffer size
     */
    private static final int COPY_BUFFER_SIZE = 4096;
    /**
     * number of times trying to create a directy(ies)
     */
    private static final int MAX_DIRECTORY_CREATION_TRIES = 5;
    /**
     * Time to sleep between tries to create a directory(ies)
     */
    private static final int SLEEP_TIME_BETWEEN_DIRECTORY_CREATION_TRIES = 100;

    /**
     * Creates a copy of a file
     * 
     * @param source
     *            the source file
     * @param target
     *            the target file
     * @return true if written successfully
     */
    public static boolean copyFile(File source, File target) {
        boolean backup = true;
        try {
            byte[] buf = new byte[COPY_BUFFER_SIZE];
            FileInputStream fis = new FileInputStream(source);
            OutputStream fos = createOutputStream(target);
            int len;
            do {
                len = fis.read(buf);
                if (len > 0) {
                    fos.write(buf, 0, len);
                }
            } while (len > 0);
            fis.close();
            fos.close();
        } catch (Exception e) {
            backup = false;
        }
        return backup;
    }

    /**
     * Creates a file output stream. This creates directories and overwriting
     * possible read-only flag
     * 
     * @param file
     *            the file
     * @return the file output stream
     * @throws FileNotFoundException
     *             if file cannot be created
     */
    public static OutputStream createOutputStream(File file) throws FileNotFoundException {
        File parent = file.getParentFile();
        if (!parent.exists()) {
            createDirectories(parent);
        }
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file);
        } catch (IOException e) {
            if (file.exists()) {
                file.delete();
            }
            fos = new FileOutputStream(file);
        }
        return new BufferedOutputStream(fos);
    }

    /**
     * Create directories safely in a multiple instances case
     * @param path
     * @param access
     * @return boolean pass/fail
     */
    public static boolean createDirectories(File path) {

        boolean retVal = true;

        int tries = 0;

        if (path.exists()) {
            return retVal;
        }

        // try few times
        while (tries < MAX_DIRECTORY_CREATION_TRIES) {

            retVal = path.mkdirs();
            // mkdirs can fail for a number of reasons including the case where
            // the directory is open by another process. The API does not make
            // any difference so we assume the latter case (worst case)
            if (!retVal) { // sleep for moment and try again.

                tries++;

                try {
                    Thread.sleep(SLEEP_TIME_BETWEEN_DIRECTORY_CREATION_TRIES);
                } catch (InterruptedException e) {
                    // Do nothing
                }
            } else { // pass
                break;
            }
        }

        return retVal;

    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File source, File target)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target)
  6. copyFile(File source, File target, boolean createParents, FileFilter filter)
  7. copyFile(File source, File target, boolean deleteSourceAfter)
  8. copyFile(File source, File target, boolean replaceIfExists)
  9. copyFile(File sourceDir, File destDir, String filename)