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) ~ ~