匹配字符串与文件名

时间:2016-08-19 18:04:56

标签: java string search document title

我正在编写一个程序来执行各种数据分析功能,以便与Excel一起使用。

我需要一种方法来返回文件的文件名,以便我可以搜索它们并找到我想要的文件。

我需要能够获取一个字符串,保存为变量,并使用它返回文件名包含该字符串的文件夹中每个文档的名称。

这将用于筛选预先分类的数据部分。理想情况下,我会将这些文档的文件名保存在字符串数组中,以便以后在其他函数中使用。

3 个答案:

答案 0 :(得分:1)

private List<String> searchForFileNameContainingSubstring( String substring )
{
   //This is assuming you pass in the substring from input.
   File file = new File("C:/Users/example/Desktop"); //Change this to the directory you want to search in.

   List<String> filesContainingSubstring = new ArrayList<String>();

   if( file.exists() && file.isDirectory() )
   {
       String[] files = file.list(); //get the files in String format.
       for( String fileName : files )
       {
           if( fileName.contains( substring ) ) 
                filesContainingSubstring.add( fileName );
       }
   }

   for( String fileName : filesContainingSubstring )
   {
      System.out.println( fileName ); //or do other operation 
   }

   return filesContainingSubstring; //return the list of filenames containing substring.
}

使用此方法,您可以将用户的输入作为您希望文件名包含的字符串传递。您需要更改的唯一其他内容是您希望在目录中开始搜索文件的位置,并且此程序仅查找该目录。

您可以从起点开始在其他目录中进一步递归查看,但我不会在此处添加该功能。你应该好好调查一下。

这也假设您正在寻找目录中的所有内容,包括其他文件夹而不仅仅是文件。

答案 1 :(得分:1)

您可以获取目录中所有文件的列表,然后将它们存储在一个数组中。接下来,使用java.io.File.getName()方法,您可以获取文件的名称。现在,您只需使用.indexOf()方法检查字符串是否是文件名的子字符串。我假设所关注目录中的所有项目都是文件而不是子目录。

public static void main(String[] args) throws IOException {
    File[] files = new File("X:/").listFiles(); //X is the directory
    String s <--- the string you want to check filenames with
    for(File f : files){
        if(f.getName().toLowerCase().indexOf(s.toLowerCase()) != -1)
        System.out.println(f.getName());
    }
}    

这应显示名称中包含字符串X:\的目录s中所有这些文件的名称。

参考

  1. 这个问题:How do I iterate through the files in a directory in Java?

  2. The java.io.File.getName() method

  3. 法定编辑信息

    我编辑这个答案只是为了替换以前的算法,用于检查字符串中是否存在子字符串,以及上面代码中当前使用的字符串。

答案 2 :(得分:1)

这是一个递归搜索文件的答案??

String name; //to hold the search file name

public String listFolder(File dir) {
    int flag;
    File[] subDirs = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    });
    System.out.println("File of Directory: " + dir.getAbsolutePath());
    flag = Listfile(dir);
    if (flag == 0) {
        System.out.println("File Found in THe Directory: " + dir.getAbsolutePath());
        Speak("File Found in THe Directory: !!" + dir.getAbsolutePath());
        return dir.getAbsolutePath();
    }
    for (File folder : subDirs) {
        listFolder(folder);
    }
    return null;
}

private int Listfile(File dir) {
    boolean ch = false;
    File[] files = dir.listFiles();
    for (File file : files) {
        Listfile(file);
        if (file.getName().indexOf(name.toLowerCase()) != -1) {//check all in lower case
            System.out.println(name + "Found Sucessfully!!");
            ch = true;

        }
    }
    if (ch) {
        return 1;
    } else {
        return 0;
    }
}