在E:/desktop/1文件夹下有很多文件,见下
需要对这些log文件进行处理,其中有些log文件中包含了很多无用信息,对于这些包含多无用信息的log文件处理是将这些文件删除掉。文件中的内容见下,比如
下面是我写的程序,但不能删除文件,求大神指点下!
谢谢。。。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class ProcessMain {
static String str = "";
public static void main(String[] args) throws Exception{
String path = "E:/desktop/1";
getDirList(path);
}
public static void read(String filepath)throws Exception{
//read folder and iterate every subfile
File f = new File(filepath);
FileReader reader = new FileReader(f);
BufferedReader br = new BufferedReader(reader);
while((str = br.readLine()) != null){
if(str.contains("NoID")){
f.delete();
}
}
br.close();
}
public static void getDirList(String path) throws Exception {
File f = new File(path);
if(f != null){
if(f.isDirectory()){
File[] fs = f.listFiles();
for (File file : fs) {
if (file.isFile()) {
if(file.length() > 0)
read(file.toString());
}
else
getDirList(file.getPath());
}
}
}
}
}