Android File Copy copyFile(String from, String to)

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

Description

Copies one file to another

License

Apache License

Parameter

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

Return

true if successful, false otherwise.

Declaration

public static boolean copyFile(String from, String to) 

Method Source Code

/*/* www  . jav  a 2s.co  m*/
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. copyFile(File sourceFile, File destFile)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dst)
  4. copyFile(Path source, Path target)
  5. copyFile(String from, String target)
  6. copyFile(String oldPath, String newPath)
  7. copyFile(String origPath, String destPath)
  8. copyFile(String sourceFileName, String destinationFileName)
  9. copyFile(String sourcePath, String destPath)