文件copy

複製目錄文件函數

public static boolean copyFolder(File srcFile, File destFile) {
        if (!srcFile.isDirectory()) {
            return false;
        }
        if (!destFile.exists() && !destFile.mkdirs()) {
            return false;
        }
        boolean result = true;
        File[] list = srcFile.listFiles();
        if (list == null) {
            return result;
        }
        for (File f:list) {
            if (f.isDirectory()) {
                result &= copyFolder (f,new File(destFile,f.getName()));
            } else {
                result &= copyFile(f,new File(destFile,f.getName()));
            }
        }
        return result;
    }
    public static boolean copyFile(File srcFile, File destFile) {
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                result = copyToFile(in, destFile);
            } finally  {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    }
    public static boolean copyToFile(InputStream inputStream, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            FileOutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.flush();
                try {
                    out.getFD().sync();
                } catch (IOException e) {
                }
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }


C


static void file_copy(const char *src, const char *des){
    FILE *in ,*out;
    char buf[4096];
    
    if ((in=fopen(src,"rb"))==NULL) {
        LOGE("Can't open %s\n", src);
        return;
    } 
    if((out=fopen(des,"wb"))==NULL){
        LOGE("Can't open %s\n", des);
        return;
    }
    while(fgets(buf, sizeof(buf), in)){
        fputs(buf, out);
    } 
    check_and_fclose(in, src);
    check_and_fclose(out, des);    
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章