列出Java中目录和子目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3008043/
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
list all files from directories and subdirectories in Java
提问by Adnan
What would be the fastest way to list the names of files from 1000+ directories and sub-directories?
从 1000 多个目录和子目录中列出文件名称的最快方法是什么?
EDIT; The current code I use is:
编辑; 我目前使用的代码是:
import java.io.File;
public class DirectoryReader {
static int spc_count=-1;
static void Process(File aFile) {
spc_count++;
String spcs = "";
for (int i = 0; i < spc_count; i++)
spcs += " ";
if(aFile.isFile())
System.out.println(spcs + "[FILE] " + aFile.getName());
else if (aFile.isDirectory()) {
System.out.println(spcs + "[DIR] " + aFile.getName());
File[] listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
} else {
System.out.println(spcs + " [ACCESS DENIED]");
}
}
spc_count--;
}
public static void main(String[] args) {
String nam = "D:/";
File aFile = new File(nam);
Process(aFile);
}
}
采纳答案by Romain Hippeau
This looks fine (Recursively going through the directory) The bottleneck will be all the file i/o you need to do, optimizing your Java will not show any real improvements.
这看起来不错(递归遍历目录)瓶颈将是您需要做的所有文件 i/o,优化您的 Java 不会显示任何真正的改进。
回答by Powerlord
Until Java 7 introduces the new java.nio.fileclasses (like DirectoryStream
), I'm afraid what you already have will be the fastest.
在 Java 7 引入新的java.nio.file类(如DirectoryStream
)之前,恐怕您已经拥有的将是最快的。
回答by Alexander Pogrebnyak
The only improvement is to get rid of static spc_count
and pass spcs
string as a parameter to Process.
唯一的改进是摆脱static spc_count
并将spcs
字符串作为参数传递给Process。
public static void main(String[] args) {
String nam = "D:/";
File aFile = new File(nam);
Process("", aFile);
}
And when doing recursive call, do
并且在进行递归调用时,请执行
static void Process( String spcs, File aFile) {
...
Process(spcs + " ", listOfFiles[i]);
...
}
This way you can call this method from more than 1 thread.
通过这种方式,您可以从 1 个以上的线程调用此方法。
回答by Aksel Willgert
As this answer shows up on top of google, i'm adding a java 7 nio solution for listing all files and directories, it is takes about 80% less time on my system.
当这个答案出现在 google 之上时,我正在添加一个 java 7 nio 解决方案来列出所有文件和目录,它在我的系统上花费的时间减少了大约 80%。
try {
Path startPath = Paths.get("c:/");
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
System.out.println("Dir: " + dir.toString());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("File: " + file.toString());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
回答by Peter
If you're open to using a 3rd party library, check out javaxt-core. It includes a multi-threaded recursive directory search that should be faster than iterating through one directory at a time. There are some examples here:
如果您愿意使用 3rd 方库,请查看javaxt-core。它包括一个多线程递归目录搜索,应该比一次遍历一个目录更快。这里有一些例子:
http://www.javaxt.com/javaxt-core/io/Directory/Recursive_Directory_Search
http://www.javaxt.com/javaxt-core/io/Directory/Recursive_Directory_Search
回答by bhavya joshi
I have written a much simpler code....Try this... It will show every folder, subfolders and files...
我写了一个更简单的代码......试试这个......它会显示每个文件夹,子文件夹和文件......
int Files=0,Directory=0,HiddenFiles=0,HiddenDirectory=0;
public void listf(String directoryName){
File file=new File(directoryName);
File[] fileList=file.listFiles();
if(fileList!=null){
for(int i=0;i<fileList.length;i++){
if(fileList[i].isHidden()){
if(fileList[i].isFile())
{
System.out.println(fileList[i]);
HiddenFiles++;
}
else{
listf(String.valueOf(fileList[i]));
HiddenDirectory++;
}
}
else if (fileList[i].isFile()) {
//System.out.println(fileList[i]);
Files++;
}
else if(fileList[i].isDirectory()){
Directory++;
listf(String.valueOf(fileList[i]));
}
}
}
}
public void Numbers(){
System.out.println("Files: "+Files+" HiddenFiles: "+HiddenFiles+"Hidden Directories"+HiddenDirectory+" Directories: "+Directory);`
}