用于搜索文件夹树并查找特定文件类型的递归方法 [英] Recursive method to search through folder tree and find specific file types
问题描述
所以我正在编写一个代码来查找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
推荐答案
- java.io.File 是文件的抽象表示形式目录路径名
- File.listFiles 提供目录中包含的所有文件的列表(如果
文件
对象代表一个目录) - 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");
您可以检查文件
是否为目录... ...
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
测试文件实际存在 -
File.isFile
,基本上不是说!File.isDirectory()
-
File.getName()
,返回文件名,不包括文件名路径 -
File.getPath()
返回文件的路径和名称。这可能是相对的,所以要小心,请参阅File.getAbsolutePath
和File.getCanonicalPath
来解决此问题。 -
File.getParentFile
,可让您访问父文件夹
File.exists
to test that the file actually existsFile.isFile
, basically instead of saying!File.isDirectory()
File.getName()
, returns the name of the file, excluding it's pathFile.getPath()
returns the path and name of the file. This can be relative, so be careful, seeFile.getAbsolutePath
andFile.getCanonicalPath
to resolve this.File.getParentFile
which gives you access to the parent folder
这篇关于用于搜索文件夹树并查找特定文件类型的递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!