用于搜索文件夹树并查找特定文件类型的递归方法 [英] Recursive method to search through folder tree and find specific file types

查看:181
本文介绍了用于搜索文件夹树并查找特定文件类型的递归方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个代码来查找Protein数据库中的某些信息。我知道递归文件夹搜索是查找这些文件的最佳方式,但我对这种语言很新,并且被告知用Java编写(我通常做C ++)

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:

首先:在桌面上找到文件夹

第二:打开每个文件夹和文件夹子文件夹

第三:找到以.dat类型结尾的文件(因为这些是唯一存储了蛋白质信息的文件

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. java.io.File 文件的抽象表示形式目录路径名

  2. File.listFiles 提供目录中包含的所有文件的列表(如果文件对象代表一个目录)

  3. File.listFiles(FileFilter)使您能够过滤基于文件列表根据您的需求

  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

这篇关于用于搜索文件夹树并查找特定文件类型的递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆