Java implementation of fuzzy matching of files in a folder and delete examples of functionality

  • 2021-01-19 22:14:10
  • OfStack

This article illustrates the ability of Java to vaguely match and delete files in a folder. To share with you for your reference, 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 PM
 * @version V1.0
 */
public class DelFiles {
 /**
 * @Title: main
 * @Description:
 * @param args
 * @return void
 * @author lisr
 * @date Mar 7, 2012 5:36:04 PM
 * @throws
 */
 // Used to blur to remove the header as str The file 
 public static boolean delFilesByPath(String path,String str){
 // Parameters that ---------path: The path to the folder of the file to be deleted ---------str: The header of the string to match 
 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(" The file name to be deleted :"+tempFile[i].getName());
 boolean del=deleteFile(path+tempFile[i].getName());
 if(del){
  System.out.println(" file "+tempFile[i].getName()+" Delete the success ");
  b=true;
 }else{
  System.out.println(" file "+tempFile[i].getName()+" Delete failed ");
 }
 }
 }
 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/";
 String str="44_";
 if(delFilesByPath(path,str)){
 System.out.println(path+" Contained in the "+str+" All files have been deleted successfully !");
 }else{
 System.out.println(path+" Contained in the "+str+" The file has failed to be deleted or does 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 PM
 * @version V1.0
 */
public class DelFiles {
 /**
 * @Title: main
 * @Description:
 * @param args
 * @return void
 * @author lisr
 * @date Mar 7, 2012 5:36:04 PM
 * @throws
 */
 // Used to blur to remove the header as str The file 
 public static boolean delFilesByPath(String path,String str){
 // Parameters that ---------path: The path to the folder of the file to be deleted ---------str: The header of the string to match 
 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(delFilesByPath(path,str)){
 System.out.println(path+" Contained in the "+str+" All files have been deleted successfully !");
 }else{
 System.out.println(path+" Contained in the "+str+" The file has failed to be deleted or does not exist in this folder !");
 }
 }
}

In my opinion: if you want to achieve more advanced such fuzzy matching, you only need to use String indexOf() Method, any file containing this string, 1 and delete!

More about java algorithm related content interested readers can see the site: Java file and directory operation tips summary, Java data structure and algorithm tutorial, Java operation DOM node tips summary and Java cache operation tips summary

I hope that what this article describes is helpful to java program design.


Related articles: