Java delete files and folders specific implementation

  • 2020-04-01 02:22:06
  • OfStack

1. Verify whether the incoming path is the correct path name (Windows system, not used by other systems)
Java code

//Verify that the string is a regular expression & PI for the correct path name;
private static String matches = "[A-Za-z]:\\[^:?"><*]*";  
//Check whether this is correct by the return value of the spath.matches (matches) method & PI;
//SPath is the path string & PI;

2, general folder or file deletion method, directly call this method, you can achieve the deletion of folders or files, including all files under the folder
Java code

  
public boolean DeleteFolder(String sPath) {  
    flag = false;  
    file = new File(sPath);  
    //Determining whether a directory or file exists & NBSP;
    if (!file.exists()) {  //There is no return false 
        return flag;  
    } else {  
        //Judge whether or not it is a document & NBSP;
        if (file.isFile()) {  //Call the delete file method & NBSP; when a file is called;
            return deleteFile(sPath);  
        } else {  //The delete directory method & PI is called when a directory is called;
            return deleteDirectory(sPath);  
        }  
    }  
}  

3. Implement the method of deleting files.
Java code

  
public boolean deleteFile(String sPath) {  
    flag = false;  
    file = new File(sPath);  
    //If the path is a file and is not empty, it is deleted & NBSP;
    if (file.isFile() && file.exists()) {  
        file.delete();  
        flag = true;  
    }  
    return flag;  
}  

4. Implement the method of deleting folders,
Java code

  
public boolean deleteDirectory(String sPath) {  
    //If sPath does not end with a file separator, the file separator & NBSP; is automatically added.
    if (!sPath.endsWith(File.separator)) {  
        sPath = sPath + File.separator;  
    }  
    File dirFile = new File(sPath);  
    //Exit if dir does not exist or is not a directory.
    if (!dirFile.exists() || !dirFile.isDirectory()) {  
        return false;  
    }  
    flag = true;  
    //Deletes all files in the folder (including subdirectories)& NBSP;
    File[] files = dirFile.listFiles();  
    for (int i = 0; i < files.length; i++) {  
        //Deletes subfiles & NBSP;
        if (files[i].isFile()) {  
            flag = deleteFile(files[i].getAbsolutePath());  
            if (!flag) break;  
        } //Delete subdirectory & NBSP;
        else {  
            flag = deleteDirectory(files[i].getAbsolutePath());  
            if (!flag) break;  
        }  
    }  
    if (!flag) return false;  
    //Delete the current directory & NBSP;
    if (dirFile.delete()) {  
        return true;  
    } else {  
        return false;  
    }  
}  

5. The main() method
Java code

public static void main(String[] args) {  
    HandleFileClass hfc = new HandleFileClass();  
    String path = "D:\Abc\123\Ab1";  
    boolean result = hfc.CreateFolder(path);  
    System.out.println(result);  
    path = "D:\Abc\124";  
    result = hfc.DeleteFolder(path);  
    System.out.println(result);  
}  

The main() method just did a simple test, create folders and files are created locally, the case should be considered very comprehensive, including folders containing folders, files. Different circumstances of the document...
There is no problem with the implementation and folders and files can be deleted correctly.

Related articles: