Java-implemented fuzzy match of files in a folder and delete function example, java example
This example describes how to use Java to perform fuzzy match and delete a file in a folder. We will share this with you for your reference. The details are as follows:
Package com. wyebd. gis; import java. io. file;/*** @ Title: DelFiles. java * @ Package com. wyebd. gis * @ Description: * @ author lisr * @ date Mar 7, 2012 5:36:03 * @ version V1.0 */public class DelFiles {/*** @ Title: main * @ Description: * @ param args * @ return void * @ author lisr * @ date Mar 7, 2012 5:36:04 PM * @ throws * // The public static boolean delFilesByPath (String path, String str) used to blur the file whose header is str ){ // Parameter description --------- path: path of the folder of the File to be deleted --------- str: boolean B = false; file File = new File (path ); file [] tempFile = file. listFiles (); for (int I = 0; I <tempFile. length; I ++) {if (tempFile [I]. getName (). startsWith (str) | tempFile [I]. getName (). endsWith (str) {System. out. println ("name of the file to be deleted:" + tempFile [I]. getName (); boolean del = deleteFile (path + tempFile [I]. getName (); if (del) {System. out. println ("file" + TempFile [I]. getName () + "deleted successfully"); B = true;} else {System. out. println ("file" + tempFile [I]. getName () + "failed to delete") ;}} return B;} private static boolean deleteFile (String path) {System. out. println (path); boolean del = false; File file = new File (path); if (file. isFile () {file. delete (); del = true;} return del;} public static void main (String [] args) {// TODO Auto-generated method stub String path = "D: /temp/"; St Ring str = "44 _"; if (delFilesByPath (path, str) {System. out. all println (path + "files containing" + str + "have been deleted successfully! ");} The else {System. out. println (path +" contains "+ str +" files that have failed to be deleted or do not exist in this folder! ");}}}
Package com. wyebd. gis; import java. io. file;/*** @ Title: DelFiles. java * @ Package com. wyebd. gis * @ Description: * @ author lisr * @ date Mar 7, 2012 5:36:03 * @ version V1.0 */public class DelFiles {/*** @ Title: main * @ Description: * @ param args * @ return void * @ author lisr * @ date Mar 7, 2012 5:36:04 PM * @ throws * // The public static boolean delFilesByPath (String path, String str) used to blur the file whose header is str ){ // Parameter description --------- path: path of the folder of the File to be deleted --------- str: boolean B = false; file File = new File (path ); file [] tempFile = file. listFiles (); for (int I = 0; I <tempFile. length; I ++) {if (tempFile [I]. getName (). startsWith (str) | tempFile [I]. getName (). endsWith (str) {tempFile [I]. delete (); B = true;} return B;} public static void main (String [] args) {String path = "D:/temp /"; string str = "44 _"; if (delFiles ByPath (path, str) {System. out. println (path + "contains" + str + ", all files have been deleted successfully! ");} The else {System. out. println (path +" contains "+ str +" files that have failed to be deleted or do not exist in this folder! ");}}}
I personally think: To achieve more advanced fuzzy match, you only need to use the StringindexOf()
Methods. All files containing this string are deleted!