Java uses recursion to get the specified file path directory, removing the specified file suffix (extensible, configured to remove the suffix according to the specific needs) ~ ~

Source: Internet
Author: User

In the work development process, each time with SVN submit code full selection, found that will produce a lot of unwanted file suffix junk file, feel very annoying, a delete too troublesome, if produce a variety of suffix file, that delete more laborious, right? Think, wrote a program through recursive processing, although there are a lot of such code, but I feel that there are problems, the online code a lot of need to take over to execute a bit to know if there is a problem, if it is a multi-level directory structure, will be your same file name also killed, And I wrote this. Even the file suffix name that needs to be deleted is not deleted, as is the filename. I execute the commit before each commit, so there will be no unwanted suffix files. This is still more practical, share to everyone, hope to let more people, improve development efficiency.

Package test;
Import Java.io.File;
Import java.util.ArrayList;
Import java.util.List;
public class Sweepunusedfiles
{
public static void Main (string[] args)
{
String Filedir = "f:\\ a directory";
list<string> suffixlist = new arraylist<string> ();
Suffixlist.add (". db");
Suffixlist.add (". tmp");
Suffixlist.add (". Html_zh");
Suffixlist.add ("_zh.js");
Sweepunusedfiles sweepunusedfiles = new Sweepunusedfiles ();
Sweepunusedfiles.startdeletefixedfiles (Filedir, suffixlist);
System.out.println ("Execution done! ");
}
public void Startdeletefixedfiles (String filedir, list<string> suffixlist)
{
if (null = = Filedir | | ". Equals (Filedir.trim ()))
{
System.out.println ("Filedir directory is not right! ");
Return
}
Filedir = Filedir.trim ();
if (null = = Suffixlist | | suffixlist.size () <= 0)
{
System.out.println ("suffixlist no suffix to match! ");
Return
}
File F = new file (Filedir);
if (F.isdirectory ())
{
Handlefile (f, suffixlist);
}
Else
{
SYSTEM.OUT.PRINTLN ("Filedir must be a directory");
/* for (String suffix:suffixlist) {if (F.getname (). EndsWith (suffix)) {//matches to delete try {f.delete ();}
* catch (Exception e) {System.out.println ("File deletion failed:" + f.getabsolutepath () + "\ \" + f.getname ());}} } */
}
}
private void Handlefile (File filedir, list<string> suffixlist)
{
Directory
file[] files = filedir.listfiles ();
for (File subfile:files)
{
if (Subfile.isdirectory ())
{
Handlefile (Subfile, suffixlist);
}
Else
{
File
for (String suffix:suffixlist)
{
if (Subfile.getname (). EndsWith (suffix))
{
Match to the To delete
Try
{
Subfile.delete ();
System.out.println ("Deleted files:" + subfile.getabsolutepath () + "\ \" + subfile.getname ());
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("File deletion failed:" + subfile.getabsolutepath () + "\ \" + subfile.getname ());
}
}
}
}
}
}
}

PS: See effect

Java uses recursion to get the specified file path directory, removing the specified file suffix (extensible, configured to remove the suffix according to the specific needs) ~ ~

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.