之前写过
FileHelper类,其中的readFile和writeFile方法分别用于文件的读和写。这次在原来的基础上添加了如下方法:
- listFiles
- hasWords
- findFilesContainsWords
- 递归地查找某一目录下所有包含某一关键字的所有文件(这里我加了一个过滤器,即我只找了所有后缀为“.h”的文件)。
加了新功能后的FileHelper类代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class FileHelper {
public static String readFile(String filename) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String ans = "", line = null;
while((line = reader.readLine()) != null){
ans += line + "\r\n";
}
reader.close();
return ans;
}
public static void writeFile(String content, String filename) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(content);
writer.flush();
writer.close();
}
public static void listFiles(String path) {
File file = new File(path);
File[] files = file.listFiles();
if(files == null)
return;
for(File f : files) {
if(f.isFile()) {
System.out.println(f.toString());
} else if(f.isDirectory()) {
System.out.println(f.toString());
listFiles(f.toString());
}
}
}
public static boolean hasWords(String file, String words) {
try {
String s = readFile(file);
int w_len = words.length();
int len = s.length();
for(int i=0;i+w_len<=len;i++) {
if(s.substring(i, i+w_len).equals(words))
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void findFilesContainsWords(String path, String words) throws Exception {
File file = new File(path);
File[] files = file.listFiles();
if(files == null) return;
for(File f : files) {
if(f.isFile()) {
String s = f.toString();
int s_len = s.length();
if(s.substring(s_len-2, s_len).equals(".h") == false) continue; // add filter
if(hasWords(f.toString(), words))
System.out.println(f.toString());
} else if(f.isDirectory()) {
findFilesContainsWords(f.toString(), words);
}
}
}
public static void main(String[] args) throws Exception {
//String ans = readFile("D:\\input.txt");
//System.out.println(ans);
//writeFile(ans, "D:\\output.txt");
//findFilesContainsWords("D:\\clamav-0.98.6", "scanmanager");//在IDE中找
if(args.length != 1) {
System.out.println("Usage : \"D:\\clamav-0.98.6\" words");
return;
}
findFilesContainsWords("D:\\clamav-0.98.6", args[0]);//在命令行中找
}
}
posted on 2015-03-10 16:11
marchalex 阅读(581)
评论(0) 编辑 收藏 所属分类:
java小程序