Java 从文件夹及其子文件夹中查找特定文件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20401610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find specific file type from folder and its sub folder
提问by user2614607
I am writing a method to get specific file type such as pdf or txt from folders and subfolders but I am lacking to solve this problem. here is my code
我正在编写一种方法来从文件夹和子文件夹中获取特定文件类型,例如 pdf 或 txt,但我无法解决这个问题。这是我的代码
// .............list file
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
listf(file.getAbsolutePath());
}
}
My current method list all files but I need specific files
我当前的方法列出了所有文件,但我需要特定文件
采纳答案by Memran
if(file.getName().endsWith(".pdf")) {
//it is a .pdf file!
}
/***/
/ ***/
回答by Tim B
For a filtered list without needing recursion through sub directories you can just do:
对于不需要通过子目录递归的过滤列表,您可以执行以下操作:
directory.listFiles(new FilenameFilter() {
boolean accept(File dir, String name) {
return name.endsWith(".pdf");
}});
For efficiency you could create the FilenameFilter ahead of time rather than for each call.
为了提高效率,您可以提前创建 FilenameFilter 而不是每次调用。
In this case because you want to scan sub folders too there is no point filtering the files as you still need to check for sub folders. In fact you were very nearly there:
在这种情况下,因为您也想扫描子文件夹,所以没有必要过滤文件,因为您仍然需要检查子文件夹。事实上,你已经非常接近了:
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
if (file.getName().endsWith(".pdf")) {
System.out.println(file.getAbsolutePath());
}
} else if (file.isDirectory()) {
listf(file.getAbsolutePath());
}
}
回答by Seelenvirtuose
Use File.listFiles(FileFilter).
Example:
例子:
File[] fList = directory.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endSwith(".pdf");
}
});
回答by asafm
Try using the FilenameFilter interface in you function http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html
尝试在您的函数中使用 FilenameFilter 接口 http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html
http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/- for a code that has extention filter
http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/- 对于具有扩展过滤器的代码
回答by Karthik Prasad
You can use apache fileUtils class
您可以使用 apache fileUtils 类
String[] exte= {"xml","properties"};
Collection<File> files = FileUtils.listFiles(new File("d:\workspace"), exte, true);
for(File file: files){
System.out.println(file.getAbsolutePath());
}
回答by luca
My advice is to use FileUtilsor NIO.2.
NIO.2allows Stream with Depth-First search, for example you can print all files with a specified extension in one line of code:
我的建议是使用FileUtils或NIO.2。
NIO.2允许 Stream with Depth-First search,例如您可以在一行代码中打印具有指定扩展名的所有文件:
Path path = Path.get("/folder");
try{
Files.walk(path).filter(n -> n.toString().endsWith(".extension")).forEach(System.out::println)
}catch(IOException e){
//Manage exception
}