遍历 Java 中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10691193/
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
Iterate through all files in Java
提问by Rohit Malish
I want to make my program print huge list of all files that I have on my computer. My problem is that it only prints files from first folder of the first hard-drive, when I want it to print all files located on my computer. Any ideas what am I doing wrong here? Thanks.
我想让我的程序打印出我计算机上所有文件的巨大列表。我的问题是它只打印第一个硬盘驱动器的第一个文件夹中的文件,当我希望它打印位于我的计算机上的所有文件时。任何想法我在这里做错了什么?谢谢。
Here is code I use:
这是我使用的代码:
Main:
主要的:
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
new Searcher(file.toString().replace('\', '/')).search();
}
}
}
and Searcher class:
和搜索者类:
import java.io.File;
public class Searcher {
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
String path = file.getPath().replace('\', '/');
System.out.println(path);
if (!path.contains(".")) {
new Searcher(path + "/").search();
}
}
}
}
回答by mprivat
I just tried this and it worked for me. I did have to add one null
check and changed the directory evaluation method though:
我刚试过这个,它对我有用。不过,我确实必须添加一项null
检查并更改了目录评估方法:
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Searcher {
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList<File>();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
new Searcher(file.toString().replace('\', '/')).search();
}
}
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
if(listOfFiles == null) return; // Added condition check
for (File file : listOfFiles) {
String path = file.getPath().replace('\', '/');
System.out.println(path);
if (file.isDirectory()) {
new Searcher(path + "/").search();
}
}
}
}
回答by Hakan Serce
You should update your search method like this:
您应该像这样更新您的搜索方法:
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
String path = file.getPath().replace('\', '/');
System.out.println(path);
if (file.isDirectory()) {
new Searcher(path + "/").search();
}
}
}
回答by Tom G
If Java 7 is an option, look into the walkFileTree() method. It will allow you to visit all files and directories in a tree, which you can start from the root of your drive. Just implement a basic FileVisitor
to process the file attributes for each Path
. You can get started here.
如果 Java 7 是一个选项,请查看walkFileTree() 方法。它将允许您访问树中的所有文件和目录,您可以从驱动器的根目录开始。只需实现一个基本的FileVisitor
来处理每个Path
. 您可以从这里开始。
回答by Puce
If you're using Java SE 7, use the new file API:
如果您使用的是 Java SE 7,请使用新的文件 API:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html
http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html
回答by SantoshK
I don't know what error you are getting but I got a NPE because you are not checking for the null after the following line.
我不知道您遇到了什么错误,但我得到了一个 NPE,因为您没有在以下行之后检查空值。
File[] listOfFiles = folder.listFiles();
After changing the code as follows it seemed to run fine , I stopped it because I have a lot of files. I am assuming it will go on to the next root after the first root(c:/ in my case)
如下更改代码后,它似乎运行良好,我停止了它,因为我有很多文件。我假设它会在第一个根之后继续到下一个根(在我的情况下为 c:/)
import java.io.File;
import java.util.ArrayList; import java.util.Arrays;
导入 java.util.ArrayList; 导入 java.util.Arrays;
public class Search {
公共类搜索{
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
System.out.println(file.toString());
new Searcher(file.toString().replace('\', '/')).search();
}
}
}
class Searcher {
类搜索器{
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
if(listOfFiles!=null)
{
for (File file : listOfFiles) {
String path = file.getPath().replace('\', '/');
System.out.println(path);
if (!path.contains(".")) {
new Searcher(path + "/").search();
}
}
}
}
}
}