//FileList.java
package keywords;
/*
* fuction: returning the file list of the folder
*
* by: Gonna
*
* */
import java.io.*;
public class FileList
{
private static final String SEP = "/";
private static StringBuffer sb = new StringBuffer("");
//
public static String[] getFiles(File f) throws IOException
{
if(f.isDirectory())
{
File[] fs=f.listFiles();
for(int i=0;i<fs.length;i++)
{
getFiles(fs[i]);
}
}
else
{
sb.append(f.getPath() + SEP);
}
String s = sb.toString();
String[] list = s.split(SEP);
return list;
}
// --
public static String[] getFiles(String t) throws IOException
{
File f = new File(t);
if(f.isDirectory())
{
File[] fs=f.listFiles();
for(int i=0;i<fs.length;i++)
{
getFiles(fs[i]);
}
}
else
{
sb.append(f.getPath() + SEP);
}
String s = sb.toString();
String[] list = s.split(SEP);
return list;
}
// public static void main(String args[]){
// //FileList.output();
//
// }
}
package delete;
/*
* function: deleting the file that you want
*
* by:Gonna
*
* */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import keywords.FileList;
public class Delete {
static String target = "result.txt";//the name of file
static String dir ="datas//";//the folder that including files
public static void main(String args[]) throws IOException {
String[] dirlist = FileList.getFiles(dir);
File f;
ArrayList<String> al = new ArrayList<String>();
for (int i = 0; i < dirlist.length; i++) {
if (dirlist[i].contains(target))
{
f = new File(dirlist[i]);
f.delete();
}
al.add(dirlist[i]);
}
for (int i = 0; i < al.size(); i++)
System.out.println(al.get(i));
}
}