搜索文件夹树并查找特定文件类型的递归方法 [英] Recursive method to search through folder tree and find specific file types
问题描述
所以我正在编写一个代码来定位蛋白质数据库上的某些信息.我知道递归文件夹搜索是定位这些文件的最佳方法,但我对这种语言很陌生,并被告知用 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
感谢您提供的任何帮助
推荐答案
- java.io.File 是文件和目录路径名的抽象表示"
- File.listFiles 提供包含在目录中的所有文件的列表(如果
File
对象代表一个目录) - File.listFiles(FileFilter) 使您能够根据需要过滤文件列表
- java.io.File is "An abstract representation of file and directory pathnames"
- File.listFiles provides a listing of all the files contained within the directory (if the
File
object represents a directory) - 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");
您可以检查File
是否是一个带有...的目录
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)
文件.exists
测试文件是否确实存在文件.isFile
,基本上不用说!File.isDirectory()
File.getName()
,返回文件名,不包括路径File.getPath()
返回文件的路径和名称.这可能是相对的,所以要小心,参见File.getAbsolutePath
和File.getCanonicalPath
来解决这个问题.File.getParentFile
允许您访问父文件夹
这篇关于搜索文件夹树并查找特定文件类型的递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!