java 如何在Java中递归查找文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40540915/
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
How to find a file recursively in Java?
提问by iloveprogramming
I am wondering how I would write a recursive program to locate a file in Java that is indicated by a starting path. It should search through a tree for a specified file. If the file is found, the location of the file should be returned. This is what I have so far (not much and still needs basic cleaning up). I need to use these exact methods. I'm mostly confused with what goes in what methods. So I know I need to use:
我想知道如何编写递归程序来在 Java 中定位由起始路径指示的文件。它应该在树中搜索指定的文件。如果找到该文件,则应返回该文件的位置。这是我到目前为止所拥有的(不多,仍然需要基本清理)。我需要使用这些确切的方法。我主要对什么方法中的内容感到困惑。所以我知道我需要使用:
File f = new File(dirName);
String [] fileList = f.list();
File aFile = new File (dirName + "\" + fileList[i]);
if (aFile.isDirectory()) {...}
public class FindFile {
If you could help me figure out what method each of those goes in that would be an amazing help!!! I'm just not really understanding the logic of each method. I also have a driver in another class that I need to utilize.
如果你能帮我弄清楚每种方法的方法,那将是一个了不起的帮助!!!我只是不太了解每种方法的逻辑。我还有一个需要使用的另一个类的驱动程序。
/**
* This constructor accepts the maximum number of files to find.
*/
public FindFile (int maxFiles)
{
}
/**
* The parameters are the target file name to look for and the directory to start in.
* @param target = target file name, dirName = directory to start in
*/
public void directorySearch (String target, String dirName) {
File f = new File(dirName);
String [] fileList = f.list();
File aFile = new File(dirName + "\" + fileList[i]);
if (aFile.isDirectory()) {
}
else {
}
}
/**
* This accessor returns the number of matching files found.
* @return number of matching files found
*/
public int getCount () {
return -1;
}
/**
* This getter returns the array of file locations, up to maxFiles in size.
* @return array of file locations
*/
public String [] getFiles () {
return new String[] {""};
}
/**
* Prompt the user for max number of files to look for, the directory to start in, and the file name.
* Then, print out the list of found files with the full path name to the file (including starting
* directory). In the event of an exception being thrown, driver catches it and provides an appropriate
* message to the user.
*/
public static void main (String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the max number of files to look for?");
System.out.println("What directory should we start in?");
Systme.out.println("What is the file name?");
}
}
}
回答by laksys
You can use java 8 lambda features like this
您可以使用这样的 java 8 lambda 功能
Files.walk(Paths.get("your search path"))
.filter(Files::isRegularFile)
.forEach((f)->{
String file = f.toString();
if( file.endsWith("file to be searched"))
System.out.println(file + " found!");
});
回答by Saravana
You need to use recursion to search the file in all the files, directories and sub directories
您需要使用递归在所有文件、目录和子目录中搜索文件
public static void main(String[] args) {
boolean found = searchFile(new File("/tmp"), "10174");
System.out.println(found);
}
private static boolean searchFile(File file, String search) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
boolean found = searchFile(f, search);
if (found)
return true;
}
} else {
if (search.equals(file.getName())) {
return true;
}
}
return false;
}
If the file needs to returned if found
如果找到文件需要返回
static File searchFile(File file, String search) {
if (file.isDirectory()) {
File[] arr = file.listFiles();
for (File f : arr) {
File found = searchFile(f, search);
if (found != null)
return found;
}
} else {
if (file.getName().equals(search)) {
return file;
}
}
return null;
}