package jdbc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SearchString {
private static int mount = 0;
public static void main(String[] args) {
String fileName = "d:/text";
File file = new File(fileName);
args = new String[]{"pw"};
for (int i = 0; i < args.length; i++){
findFile(file, args[i]);
print(args[i]);
}
}
private static void findFile(File file, String word) {
File[] files = file.listFiles();
for(int i = 0; i < files.length; i++){
if(files[i].isDirectory()){
findFile(files[i], word);
}else {
search(files[i], word);
}
}
}
private static void search(File file, String word) {
int j = 0, k = 0, ch = 0;
String str = null;
try {
FileReader in = new FileReader(file);
while ((ch = in.read()) != -1){
str += (char)ch;
}
if(str != null){
while (str.indexOf(word, j) != -1){
k++;
j = str.indexOf(word, j) + 1;
}
}
if(k > 0){
System.out.println(file.getAbsolutePath() + k);
mount++;
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void print(String word) {
if(mount != 0){
System.out.println(mount + word);
mount = 0;
}else {
System.out.println("");
}
}
}
search String查找字符串
最新推荐文章于 2024-07-03 01:33:34 发布