[英]returning ArrayList<file> method java
When I compile I don't get any errors but the ArrayList<File>
from the method is not getting into the main. 当我编译时,我没有收到任何错误,但是方法中的ArrayList<File>
并没有进入主体。
All the entries got added into the Folders
ArrayList
but it doesn't get passed into the main method. 所有条目都添加到Folders
ArrayList
但没有传递到main方法中。
public static ArrayList<File> Process(File aFile)
{
ArrayList<File> Folders = new ArrayList<File>();
if(aFile.isFile())
{
String filenom = aFile.getPath();
if (filenom.toLowerCase().endsWith(".java")){
Folders.add(aFile);
}
}
else if (aFile.isDirectory()) {
File[] listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
}
else {
System.out.println(" [ACCESS DENIED]");
}
}
return (Folders);
}
public static void main(String[] args) throws IOException
{
ArrayList<File> FAddress = new ArrayList<File>();
File dir = new File("C:/");
FAddress = Process(dir);
if (FAddress.isEmpty())
System.out.println("WTF?!");
else{
for (File fl : FAddress) {
String FileAddress = fl.getName();
System.out.println(FileAddress);
}
}
}
The problem is with your recursion - you correctly identify if a File
object is a directory, and call Process
on it: 问题出在您的递归上-您可以正确识别File
对象是否为目录,并对其调用Process
:
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
But you forgot to add its result to the return value ( Folders
), so any result from subdirectories won't be added to the return value: 但是您忘记了将其结果添加到返回值( Folders
)中,因此子目录中的任何结果都不会添加到返回值中:
for (int i = 0; i < listOfFiles.length; i++)
Folders.addAll(Process(listOfFiles[i]));
You create 您创建
ArrayList<File> Folders = new ArrayList<File>();
in every time when run Process. 在每次运行Process时。 This function have recursion, so because you start at C:/ folder you never update this array. 此函数具有递归功能,因此因为您从C:/文件夹开始,所以您永远不会更新此数组。
for correct processing you should use something like: 为了正确处理,您应该使用类似:
public static ArrayList<File> Process(File aFile) {
ArrayList<File> Folders = new ArrayList<File>();
Process(aFile, Folders);
return folders;
}
private static void Process(File aFile, ArrayList<File> Folders) {
... //your implementation without defining Folders and returning it
}
this will be usefull if you need process many levels of tree for files. 如果您需要处理文件的许多级别的树,这将很有用。
You can define a class level variable static ArrayList<File> Folders = new ArrayList<File>();
您可以定义一个类级别的变量static ArrayList<File> Folders = new ArrayList<File>();
and then use the same for populating files. 然后将其用于填充文件。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class test {
static ArrayList<File> Folders = new ArrayList<File>();
public static void Process(File aFile) {
if (aFile.isFile()) {
String filenom = aFile.getPath();
if (filenom.toLowerCase().endsWith(".log")) {
Folders.add(aFile);
}
} else if (aFile.isDirectory()) {
File[] listOfFiles = aFile.listFiles();
if (listOfFiles != null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
} else {
System.out.println(" [ACCESS DENIED]");
}
}
}
public static void main(String[] args) throws IOException {
File dir = new File("C:/ukba/");
Process(dir);
if (Folders.isEmpty())
System.out.println("WTF?!");
else {
for (File fl : Folders) {
String FileAddress = fl.getName();
System.out.println(FileAddress);
}
}
Folders = null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.