Delete the file specified extension demo

import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class clean_suffixTest {
private static int a = 0;
public static void main(String[] args)
{
String filedir = "G:\\testpath";
List suffixList = new ArrayList ();
// suffixList.add(".avi");
suffixList.add(".MP4");
// suffixList.add(".html_zh");
// suffixList.add("_zh.js");
clean_suffixTest sweepUnusedFiles = new clean_suffixTest();
sweepUnusedFiles.startDeleteFixedFiles(filedir, suffixList);
System.out.println("执行完成!一共删除"+a+"个文件");
}
public void startDeleteFixedFiles(String filedir, List suffixlist)
{
IF (filedir == null || "" .equals (filedir.trim ()))
{
System.out.println ( "not filedir directory");!
return;
}
filedir filedir.trim = ();
IF (== null || suffixlist suffixList.size () <= 0)
{
System.out.println ( "suffixlist no suffix to match!");
return;
}
File File new new F = (filedir);
IF (F. the isDirectory ())
{
handleFile (F, suffixlist);
}
the else
{
System.out.println ( "filedir must be a directory");
(. F.getName () endsWith (suffix )): / * for (String suffix suffixList) {if {// To delete the matched {f.delete the try ();}
* the catch (Exception E) {the System.out .println ( "file deletion failed:" + f.getAbsolutePath () + "\" + f.getName ());}}} * /
}
}
Private void handleFile (file filedir, List suffixList)
{
// 目录
File[] files = filedir.listFiles();
for (File subFile : files)
{
if (subFile.isDirectory())
{
handleFile(subFile, suffixList);
}
else
{
// 文件
for (String suffix : suffixList)
{
if (subFile.getName().endsWith(suffix))
{
// 匹配到的要删除
try
{
subFile.delete();
System.out.println("已删除文件:" + subFile.getAbsolutePath() + "\" + subFile.getName());
a++;
}
the catch (Exception E)
{
System.out.println ( "document delete failed:" subFile.getAbsolutePath + () + "\" + subFile.getName ());
}
}
}
}
}
}
}

Guess you like

Origin www.cnblogs.com/Tamako/p/11486886.html