Android File Copy copyFiles(File src, File dst)

Here you can find the source of copyFiles(File src, File dst)

Description

copy files from src to dst

Parameter

Parameter Description
src a parameter
dst a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFiles(File src, File dst) throws IOException 

Method Source Code

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.Properties;

public class Main{
    /**//w  w  w.jav a  2s.co  m
     * copy files from src to dst
     * @param src
     * @param dst
     * @throws IOException
     */
    public static void copyFiles(File src, File dst) throws IOException {
        if (!src.exists())
            return;
        else if (src.isDirectory() && !dst.isFile()) {
            if (dst.exists() || dst.mkdirs()) {
                File[] subSrcs = src.listFiles();
                for (File subSrc : subSrcs) {
                    File subDst = new File(dst.getPath() + File.separator
                            + subSrc.getName());
                    copyFiles(subSrc, subDst);
                }
            }
        } else if (src.isFile() && !dst.isDirectory()) {
            if (dst.exists()
                    || (dst = createNewFile(dst.getPath())) != null)
                StreamUtil.copyThenClose(new FileInputStream(src),
                        new FileOutputStream(dst));
        } else
            return;
    }
    public static File createNewFile(String filepath) throws IOException {
        return createNewFile(new File(filepath));
    }
    /**
     * create a new blank file
     * @param file
     * @return
     * @throws IOException
     */
    public static File createNewFile(File file) throws IOException {
        File pareFile = file.getParentFile();
        if (pareFile.exists() || pareFile.mkdirs()) {
            File newFile = file;
            if (newFile.exists() || newFile.createNewFile())
                return newFile;
        }
        return null;
    }
}

Related

  1. copyFile(String srcFile, String destFile)
  2. copyFile(String strSrc, String strDest)
  3. copyFileByCommand(String src, String dst, boolean isMove)
  4. copyFileLazy(String source, String destination)
  5. copyFileToFile(File file, File outFile)
  6. copyImgFile(String sourcePath, String fileId)
  7. copyResource(Class cls, String strResSource, String strFile)
  8. copyStringToFile(String content, String FilePath)
  9. copyfile(File source, File destination)