gpt4 book ai didi

java - File.list() 与 File.listFiles()

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:43 25 4
gpt4 key购买 nike

我的问题是:这两个函数是否有不同之处?我的意思是我知道他们返回不同的东西,但是一个中的元素数量可能与第二个中的元素数量不同。我会尽力解释。我为我的一个类(class)实现了 TreeModel,试图基于 JTree 对 PC 上的文件进行漂亮的查看。所以这是它的一部分:

public Object getChild(Object parent, int index) {
File[] children = ((File) parent).listFiles();
if(children == null || index < 0 || index >= children.length) {
return null;
}

File result = new MyFile(children[index]);
return result;
}

public int getChildCount(Object parent) {
//---
//String[] children = ((File)parent).list();
File[] children = ((File)parent).listFiles();
//---

if(children == null) {
return 0;
}
return children.length;
}

我标记了有趣的代码。如果我为这个注释行更改了这两行,有时我会在加载 TreeModel 后得到 NullPointerException:jtree.setModel(treeModel);。这个未注释的不会造成任何麻烦。我检查了文档,它没有说任何异常,包括通过两种方法返回 null。这是怎么回事?

最佳答案

两种方法本质上是一样的,看http://www.docjar.com/html/api/java/io/File.java.html了解详情。

关于java - File.list() 与 File.listFiles(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15369744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com