如何读取给定目录中的所有txt文件以查找字符串? Java的

时间:2014-02-23 20:13:57

标签: java io

我希望能够扫描指定目录中的所有文本文件以查找字符串。我知道如何阅读一个文本文件。这很容易,但我如何扫描给定目录中的一堆文本文件中的所有内容?

文件将全部命名为0.txt 1.txt 2.txt等,如果这有帮助,可能使用计数器增加搜索文件的名称,然后在没有更多的txt文件时停止它?这是我最初的想法,但我似乎无法实现它

谢谢

6 个答案:

答案 0 :(得分:4)

您可以使用以下方法:

String dirName = "E:/Path_to_file";
File dir = new File(dirName);
File[] allFiles = dir.listFiles();
for(File file : allFiles)
{
     // do something
}

答案 1 :(得分:1)

此代码段将执行您要查找的内容(可能使用您选择的其他字符集):

File[] files = dir.listFiles();
for (File file : files) {
    String t_text = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    if (t_text.contains(search_string)) {
        // do whatever you want
    }
}

答案 2 :(得分:0)

您可以使用此示例代码列出目录

下的所有文件
public File[] listf(String directoryName) {

// .............list file
File directory = new File(directoryName);

// get all the files from a directory
File[] fList = directory.listFiles();
}

答案 3 :(得分:0)

然后在检索每个文件后,使用

遍历其行
LineIterator li = FileUtils.lineIterator(file);
boolean searchTermFound = false;
while(li.hasNext()  &&  !searchTermFound)  {
   String sLine = li.next();

   //Find the search term...

}

http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#lineIterator(java.io.File)

答案 4 :(得分:0)

Methodcall:scanFiles(new File("").getAbsolutePath(), "bla");

private static void scanFiles(String folderPath, String searchString) throws FileNotFoundException, IOException {
    File folder = new File(folderPath);

    //just do something if its a directory (otherwise possible nullpointerex @ Files#listFiles())
    if (folder.isDirectory()) {
        for (File file : folder.listFiles()) {
            // just scan for content if its not a directory (otherwise nullpointerex @ new FileReader(File))
            if (!file.isDirectory()) {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String content = "";
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();

                    while (line != null) {
                        sb.append(line);
                        sb.append(System.lineSeparator());
                        line = br.readLine();
                    }
                    content = sb.toString();
                } finally {
                    br.close();
                }
                if (content.contains(searchString)) {
                    System.out.println("File " + file.getName() + " contains searchString " + searchString + "!");
                }
            }
        }
    } else {
        System.out.println("Not a Directory!");
    }
}

其他信息:

您可以将FilenameFilter传递给此方法调用folder.listFiles(new FilenameFilter(){...})

答案 5 :(得分:0)

试试这个:

public class FindAllFileFromDirectory {
    static List<String> fileNames = new ArrayList<String>();

    public static void main(String[] args) {
        File AppDistributionDir = new File("<your directory path>");
        if (AppDistributionDir.isDirectory()) {
            listFilesForFolder(AppDistributionDir);
        }
        for (String s : fileNames) {
            String fileExtension = FilenameUtils.getExtension(s); // import org.apache.commons.io.FilenameUtils;
            //TODO: your condition hrere
            if (s.equals("txt")) {
                System.out.println(s);
            }
        }

    }

    public static void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                //System.out.println(fileEntry.getAbsolutePath());
                fileNames.add(fileEntry.getName());

            }
        }
    }
}