Recursive method to search through folder tree and

2019-01-20 13:03发布

问题:

So I am writing a code that locates certain information on Protein databases. I know that a recursive folder search is the best possible way to locate these files, but I am very new to this language and have been told to write in Java (I normally do C++)

SO this being said, what method would i use to:

First: Locate the folder on desktop
Second: Open each folder and that folders subfolders
Third: Locate files that end with the ".dat" type (because these are the only files that have stored the Protein information

Thanks for any and all help you can provide

回答1:

  1. java.io.File is "An abstract representation of file and directory pathnames"
  2. File.listFiles provides a listing of all the files contained within the directory (if the File object represents a directory)
  3. File.listFiles(FileFilter) provides you with the ability to filter a file list based on your needs

So, with that information...

You would specify a path location with something like...

File parent = new File("C:/path/to/where/you/want");

You can check that the File is a directory with...

if (parent.isDirectory()) {
    // Take action of the directory
}

You can list the contents of the directory by...

File[] children = parent.listFiles();
// This will return null if the path does not exist it is not a directory...

You can filter the list in a similar way...

File[] children = parent.listFiles(new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory() || file.getName().toLowerCase().endsWith(".dat");
        }
    });
// This will return all the files that are directories or whose file name ends
// with ".dat" (*.dat)

Other useful methods would include (but not limited to)

  • File.exists to test that the file actually exists
  • File.isFile, basically instead of saying !File.isDirectory()
  • File.getName(), returns the name of the file, excluding it's path
  • File.getPath() returns the path and name of the file. This can be relative, so be careful, see File.getAbsolutePath and File.getCanonicalPath to resolve this.
  • File.getParentFile which gives you access to the parent folder


回答2:

Something like this would do the trick:

public static void searchForDatFiles(File root, List<File> datOnly) {
    if(root == null || datOnly == null) return; //just for safety   
    if(root.isDirectory()) {
        for(File file : root.listFiles()) {
            searchForDatFiles(file, datOnly);
        }
    } else if(root.isFile() && root.getName().endsWith(".dat")) {
        datOnly.add(root);
    }
}

After this method returns, the List<File> passed to it will be filled with the .dat files of your directory, and all subdirectories (if i'm not mistaken).



回答3:

You should have a look at the Java File APIs. In particular you should look at the listFiles method and write FileFilter that selects directories and, of course, the files you're interested into.

A method that will return you all the files matching your criteria (Given that you implement the FileFilter) is this:

List<File> searchForFile(File rootDirectory, FileFilter filter){
    List<File> results = new ArrayList<File>();
    for(File currentItem : rootDirectory.listFiles(filter){
      if(currentItem.isDirectory()){
          results.addAll(searchForFile(currentItem), filter)
      }
      else{
          results.add(currentItem);
      }
    }
    return results;
}


回答4:

use recursive foldr search and use function endsWith() to find the .bat file then you can use any String function to locate your requires information.