Java-implemented fuzzy match of files in a folder and delete function example, java example

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.