Java implementation file copy cut file and delete examples

  • 2020-04-01 03:17:42
  • OfStack


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class FileOperateDemo {

    
    public static boolean copyGeneralFile(String srcPath, String destDir) {
        boolean flag = false;
        File file = new File(srcPath);
        if(!file.exists()) { //The source file or folder does not exist
            return false;
        }

        if(file.isFile()) {    //Copying files
            flag = copyFile(srcPath, destDir);
        }
        else if(file.isDirectory()) { //Folder copy
            flag = copyDirectory(srcPath, destDir);
        }

        return flag;
    }

    
    public static boolean copyFile(String srcPath, String destDir) {
     return copyFile(srcPath, destDir, true); //Overwrites the file with the same name by default
    }

    
    public static boolean copyDirectory(String srcPath, String destDir) {
     return copyDirectory(srcPath, destDir, true);
    }

    
    public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) {
        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isFile()) { //The source file does not exist
            return false;
        }

        //Gets the filename of the file to be copied
        String fileName = srcFile.getName();
        String destPath = destDir + File.separator +fileName;
        File destFile = new File(destPath);
        if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { //The source file path and the destination file path overlap
            return false;
        }
        if(destFile.exists() && !overwriteExistFile) {    //There are files with the same name in the target directory and overwrite is not allowed
            return false;
        }

        File destFileDir = new File(destDir);
        if(!destFileDir.exists() && !destFileDir.mkdirs()) { //The directory does not exist and the directory creation failure returns directly
         return false;
        }

        try {
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(destFile);
            byte[] buf = new byte[1024];
            int c;
            while ((c = fis.read(buf)) != -1) {
                fos.write(buf, 0, c);
            }
            fos.flush();
            fis.close();
            fos.close();

            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flag;
    }

    
    public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) {
        if(destDir.contains(srcPath))
           return false;
        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isDirectory()) { //The source folder does not exist
            return false;
        }

        //Get the name of the folder to be copied. For example, if the folder to be copied is "E:\dir ", get the name "dir"
        String dirName = srcFile.getName();

        //The full path to the destination folder
        String destDirPath = destDir + File.separator + dirName + File.separator;
        File destDirFile = new File(destDirPath);
        if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) {
         return false;
        }
        if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) {    //If the destination has a folder with the same name and overwriting is not allowed, then false is returned directly
            return false;
        }

        if(!destDirFile.exists() && !destDirFile.mkdirs()) {  //If the target directory does not exist and the directory creation failed
         return false;
        }

        File[] fileList = srcFile.listFiles();    //Gets the subfiles and subfolders under the source folder
        if(fileList.length==0) {    //If the source folder is an empty directory, set the flag to true. This step is very subtle and has been debug for a long time
            flag = true;
        }
        else {
            for(File temp: fileList) {
                if(temp.isFile()) {    //file
                    flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir);     //Recursive replication also inherits the override property
                }
                else if(temp.isDirectory()) {    //file clip 
                    flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir);   //Recursive replication also inherits the override property
                }

                if(!flag) {
                    break;
                }
            }
        }

        return flag;
    }

    
    public static boolean deleteFile(String path) {
        boolean flag = false;

        File file = new File(path);
        if (!file.exists()) { //file There is no direct return 
            return flag;
        }
        flag = file.delete();

        return flag;
    }

    
    
    public static boolean cutGeneralFile(String srcPath, String destDir) {
     boolean flag = false;
        if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { //Copy and delete were successful
         flag = true;
        }

        return flag;
    }

    public static void main(String[] args) {
     
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  //  Normal scenario 
     System.out.println(copyGeneralFile("d://notexistfile", "d://test/"));      //  Copy a file or folder that does not exist 
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/"));      //  The files to be copied are in the same directory as the destination files 
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  //  Overwrite the file of the same name in the target directory 
     System.out.println(copyFile("d://test/", "d://test2", false)); //  Does not overwrite a file of the same name in the target directory 
     System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); //  Copy files to a directory that cannot exist or be created 

     System.out.println("---------");

     
     System.out.println(copyGeneralFile("d://test/", "d://test2/"));

     System.out.println("---------");

     
     System.out.println(deleteFile("d://a/"));
    }

}


Related articles: