Android File Copy copy(File sourceFile, File targetFile)

Here you can find the source of copy(File sourceFile, File targetFile)

Description

Copy the source file to the target file.

License

Apache License

Parameter

Parameter Description
sourceFile The source file to copy from
targetFile The target file to copy to

Exception

Parameter Description
FileAlreadyExistsException Thrown if the target file alreadyexists. This exception is a subclass of IOException, so catchingan IOException is enough if you don't care about this specific reason.
IOException Thrown if an error during the copy

Declaration

public static void copy(File sourceFile, File targetFile)
        throws FileAlreadyExistsException, IOException 

Method Source Code

/*//www  .ja v  a 2s  . c o m
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * 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.
 * #L%
 */

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class Main{
    /**
     * Copy the source file to the target file.
     * @param sourceFile The source file to copy from
     * @param targetFile The target file to copy to
     * @throws FileAlreadyExistsException Thrown if the target file already
     *      exists.  This exception is a subclass of IOException, so catching
     *      an IOException is enough if you don't care about this specific reason.
     * @throws IOException Thrown if an error during the copy
     */
    public static void copy(File sourceFile, File targetFile)
            throws FileAlreadyExistsException, IOException {
        copy(sourceFile, targetFile, false);
    }
    /**
     * Copy the source file to the target file while optionally permitting an
     * overwrite to occur in case the target file already exists.
     * @param sourceFile The source file to copy from
     * @param targetFile The target file to copy to
     * @return True if an overwrite occurred, otherwise false.
     * @throws FileAlreadyExistsException Thrown if the target file already
     *      exists and an overwrite is not permitted.  This exception is a
     *      subclass of IOException, so catching an IOException is enough if you
     *      don't care about this specific reason.
     * @throws IOException Thrown if an error during the copy
     */
    public static boolean copy(File sourceFile, File targetFile,
            boolean overwrite) throws FileAlreadyExistsException,
            IOException {
        boolean overwriteOccurred = false;

        // check if the targetFile already exists
        if (targetFile.exists()) {
            // if overwrite is not allowed, throw an exception
            if (!overwrite) {
                throw new FileAlreadyExistsException("Target file "
                        + targetFile + " already exists");
            } else {
                // set the flag that it occurred
                overwriteOccurred = true;
            }
        }

        // proceed with copy
        FileInputStream fis = new FileInputStream(sourceFile);
        FileOutputStream fos = new FileOutputStream(targetFile);
        fis.getChannel().transferTo(0, sourceFile.length(),
                fos.getChannel());
        fis.close();
        fos.flush();
        fos.close();

        return overwriteOccurred;
    }
}

Related

  1. copy(File inFile, File outFile)
  2. copy(File pSourceFile, File pTargetFile)
  3. copy(File source, File target, boolean append)
  4. copy(File sourceFile, File destinationFile)
  5. copy(File sourceFile, File targetFile, boolean overwrite)
  6. copy(File sourceFile, String destinction)
  7. copy(File src, File dest)
  8. copy(String input, String output)