Android File Copy copyFile(File from, File to)

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

Description

Copies one file to another, overwriting the destination file.

License

Apache License

Parameter

Parameter Description
from the file to be copied.
to the destination file.

Return

true if successful, false otherwise.

Declaration

public static boolean copyFile(File from, File to) 

Method Source Code

/*/*from w w  w. ja v  a2 s.  c  om*/
Copyright 2008 Flaptor (flaptor.com) 

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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.log4j.Logger;

public class Main{
    private static Logger logger = Logger.getLogger(Execute.whoAmI());
    /**
     * Copies one file to another, optionally appending.
     * @param from the file to be copied.
     * @param to the destination file.
     * @return true if successful, false otherwise.
     */
    public static boolean copyFile(File from, File to, boolean append) {
        boolean ok = true;
        try {
            BufferedInputStream in = new BufferedInputStream(
                    new FileInputStream(from), 64 * 1024);
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(to, append), 64 * 1024);
            byte[] buf = new byte[8 * 1024];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            logger.warn("Copying file " + from.getName() + " to "
                    + to.getName() + ": " + e);
            ok = false;
        }
        return ok;
    }
    /**
     * Copies one file to another, overwriting the destination file.
     * @param from the file to be copied.
     * @param to the destination file.
     * @return true if successful, false otherwise.
     */
    public static boolean copyFile(File from, File to) {
        return copyFile(from, to, false);
    }
    /**
     * Copies one file to another
     * @param from the name of the file to be copied.
     * @param to the name of the destination file.
     * @return true if successful, false otherwise.
     */
    public static boolean copyFile(String from, String to) {
        return copyFile(new File(from), new File(to));
    }
}

Related

  1. copy(String pSourceFile, String pTargetFile)
  2. copy(String source, String destinction)
  3. copy(String srcFileName, String destFileName)
  4. copyBundleFile(IPath projectRelativePath, String outputPath)
  5. copyFile(File fin, File fout)
  6. copyFile(File from, File to, boolean append)
  7. copyFile(File from, File to, byte[] buf)
  8. copyFile(File in, File out)
  9. copyFile(File source, File dest)