2014-09-02 65 views
-1

我目前有一个Java程序,它循环浏览Windows机器上的目录中的数百个文件,在每个文件中搜索字符串。Wild Card在Java中搜索文件系统文件夹

我这样做是为了从200多个文件名中创建一个数组,并且程序执行没有问题。

我想知道,如果可能要么

A.使用通配符所以每次我改变我寻遍我不要的文件时必须列出了200多个文件,在我的代码的数组。

B只搜索特定文件夹内的所有文件。

下面是我的代码,其中inputFile是文件数组。

try { 
      br = new BufferedReader(new FileReader(inputFile[i])); 
      try { 
       while((line = br.readLine()) != null) 
       { 
        countLine++; 
        //System.out.println(line); 
        String[] words = line.split(" "); 

        for (String word : words) { 
         if (word.equals(inputSearch)) { 
          count++; 
          countBuffer++; 
         } 
        } 

        if(countBuffer > 0) 
        { 
         countBuffer = 0; 
         lineNumber += countLine + ","; 
        } 

       } 
       br.close(); 
+0

A. [是](http://stackoverflow.com/questions/1384947/java-find-txt-files-in-specified-folder)。 B. [是](http://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java)。 – RossC 2014-09-02 13:03:56

+0

也许正在监视文件系统更改效率更高: http://docs.oracle.com/javase/tutorial/essential/io/notification.html – mariusm 2014-09-02 13:07:55

回答

0

有一种方法listFiles()其中列出了在给定的目录中的所有File秒。

File directory = new File(-Path to directory-); 
File[] files = directory.listFiles(); 

然后你可以重复一遍。但请注意,在读取文件之前,listFiles()还会提取所有需要检查的子文件夹。

+0

工作正常。谢谢....我可能会漏掉一些显而易见的东西,但我怎么知道该方法用于文件的顺序?我无法确定它是如何订购这些文件的。 – user2941841 2014-09-02 13:57:16

+0

根据[文档](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28%29):'不能保证名称字符串在结果数组中将以任何特定顺序出现;他们并不特别保证按字母顺序出现。“ – QBrute 2014-09-02 23:39:38