@[toc]

1. Delete files or folders (optional)

/** * Delete file operation (optional) **@paramFilePath specifies the absolute filePath */
synchronized public static boolean deleteFile(String filePath){
	// Create a file class
	File file = new File(filePath);
	// If the file exists
	return file.delete();
}
Copy the code

2. Obtain the volume of files or folders

/** * Get file or folder size (using recursion) **@paramFile The file or folder to be identified */
public static long getFileSize(File file) throws Exception {
	long size = 0;
    // File is the first element in the recursion.
    if(file.isDirectory()){
    	// Get the file array (folder and file)
    	File[] fileArray = file.listFiles();
    	// Depth traversal
    	for(int i = 0; i < fileArray.length; i++){
    		FileArray [I] is the second element in the recursion.
        	if(fileArray[i].isDirectory()){
            	//
            	size = size + getFileSize(fileArray[i]);
        	}
        	// If it is a file
        	else{ size = size + fileArray[i].length(); }}}// otherwise file is a file
    else {
    	//
        size = size + file.length();
    }
    return size;
}
Copy the code