java 查找目录下文件内容包含某个字符串的文件

时间:2022-12-02 15:55:17

这个java类主要是用来查找文件内容的,而不是查找文件名的。主要作用是查找目录下所有文件的文件内容包含特定字符串的文件,并打印输出位置和找到的字符数量。可以定义多个字符进行查找,不需要担心文件格式问题,非常方便!

package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SearchStrInPath
{
public static int mount = 0;
public static void main(String[] args)
{
String filename = "e:\\20161215\\wsbsbb";
//创建一个 File 实例,表示路径名是指定路径参数的文件
File file = new File(filename);
args=new String[]{"PW"};//
for (int i = 0; i < args.length; i++) {
findFile(file, args[i]);
print(args[i]);
}

}
public static boolean isTrueFile(File file)
{
if(!file.exists() || !file.canRead())
return false;
if (file.getName().startsWith("."))
return false;
if (file.getName().endsWith("."))
return false;
return true;
}
public static void findFile(File file, String word)
{
File[] listFiles = file.listFiles();
//得到一个File数组,它默认是按文件最后修改日期排序的
for (int i = 0; i < listFiles.length; i++)
{
if (listFiles[i].isDirectory())
findFile(listFiles[i], word);
else if (isTrueFile(listFiles[i]))
search(listFiles[i], word);
}
}
public static void search(File file, String word)
{
try
{
int j = 0, k = 0, ch = 0;
String str = null;
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+ " 个关键字" + word);
mount++;
}
in.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void print(String word)
{
if (mount != 0)
{
System.out.println("一共找到 " + mount + " 个文件包含关键字" + word + "! \n");
mount=0;
}
else
{
System.out.println("没有找到相应的文件");
}
}
}
java 查找目录下文件内容包含某个字符串的文件