当我从中调用displayFilesInDirectory()
时,返回列表System.out.Println()
的方法会打印整个列表。但是,调用 displayFilesInDirectory
的方法会打印" []"作为列表内容。
public void dropCompleted(DragEvent event) {
Dragboard db = event.getDragboard();
boolean success = false;
List list = new ArrayList();
String tableName;
List listOfDirectories;
tableName = (String) playList.getSelectionModel().selectedItemProperty().getValue();
if (db.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
for (Object file : db.getFiles()) {
File directories = (File) file;
listOfDirectories = (displayFilesInDirectory(directories));
System.out.println("Calling displayFilesInDirectory: " + listOfDirectories);
}
success = true;
}
public static List displayFilesInDirectory(File dir) {
List newList = new ArrayList();
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
displayFilesInDirectory(file);
} else if (file.isFile()) {
newList.add(file);
}
}
System.out.println("From displayFileInDirectory()"+newList);
return newList;
}
答案 0 :(得分:1)
如果你在一个没有直接在该目录中的文件的目录上调用它,但只在它的子目录中调用它,displayFilesInDirectory
将返回一个空列表,因为对该目录中目录的递归调用不会将结果列表添加到newList
。用
newList.addAll(displayFilesInDirectory(file));