copy Folder for various parameters - Android File Input Output

Android examples for File Input Output:Copy File

Description

copy Folder for various parameters

Demo Code


//package com.java2s;
import java.io.File;
import java.io.IOException;

public class Main {
    public static void copyFolder1(File srcFolder, File destFolder)
            throws IOException {
        if (srcFolder.isDirectory()) {
            if (destFolder.isDirectory()) {
                File[] files = srcFolder.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        File dest = new File(destFolder, files[i].getName());
                        dest.mkdir();//from  www  .  j ava 2s.c  o m
                        copyFolder(files[i], dest);
                    } else {
                        File dest = new File(destFolder, files[i].getName());
                        copyFile1(files[i], dest);
                    }
                }
            }
        }
    }

    public static void copyFolder(String src, String dest)
            throws IOException {
        File srcFolder = new File(src);
        if (srcFolder.isDirectory()) {
            File destFolder = new File(dest);
            if (destFolder.isDirectory()) {
                File[] files = srcFolder.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        File destfolder = new File(destFolder,
                                files[i].getName());
                        destfolder.mkdir();
                        copyFolder(files[i], destfolder);
                    } else {
                        File file = new File(destFolder, files[i].getName());
                        copyFile(files[i], file);
                    }
                }
            }
        }
    }

    public static void copyFolder(File srcFolder, File destFolder)
            throws IOException {
        if (srcFolder.isDirectory()) {
            if (destFolder.isDirectory()) {
                File[] files = srcFolder.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        File dest = new File(destFolder, files[i].getName());
                        dest.mkdir();
                        copyFolder(files[i], dest);
                    } else {
                        File dest = new File(destFolder, files[i].getName());
                        copyFile(files[i], dest);
                    }
                }
            }
        }
    }

    public static boolean copyFile1(String src, String dest)
            throws IOException {
        File srcFile = new File(src);
        if (srcFile.isFile()) {
            File destFile = new File(dest);
            copyFile1(srcFile, destFile);
        }
        return false;
    }

    public static boolean copyFile1(File srcFile, File destFile)
            throws IOException {
        if (srcFile.isFile()) {
            if (destFile.exists()) {
                if (destFile.lastModified() >= srcFile.lastModified()) {
                    return true;
                }
            }
            java.io.FileInputStream is = new java.io.FileInputStream(
                    srcFile);
            try {
                java.io.FileOutputStream os = new java.io.FileOutputStream(
                        destFile);
                try {
                    byte[] buf = new byte[1024 * 100];
                    int count;
                    while (true) {
                        count = is.read(buf);
                        if (count < 1)
                            break;
                        os.write(buf, 0, count);
                        if (count < buf.length)
                            break;
                    }
                } finally {
                    os.close();
                }
                destFile.setLastModified(srcFile.lastModified());
            } finally {
                is.close();
            }
            return true;
        }
        return false;
    }

    public static boolean copyFile(String src, String dest)
            throws IOException {
        File srcFile = new File(src);
        if (srcFile.isFile()) {
            File destFile = new File(dest);
            copyFile(srcFile, destFile);
        }
        return false;
    }

    public static boolean copyFile(File srcFile, File destFile)
            throws IOException {
        if (srcFile.isFile()) {
            java.io.FileInputStream is = new java.io.FileInputStream(
                    srcFile);
            try {
                java.io.FileOutputStream os = new java.io.FileOutputStream(
                        destFile);
                try {
                    byte[] buf = new byte[1024 * 100];
                    int count;
                    while (true) {
                        count = is.read(buf);
                        if (count < 1)
                            break;
                        os.write(buf, 0, count);
                        if (count < buf.length)
                            break;
                    }
                } finally {
                    os.close();
                }
                destFile.setLastModified(srcFile.lastModified());
                return true;
            } finally {
                is.close();
            }
        }
        return false;
    }
}

Related Tutorials