TreeContentProvider仅获取路径中的每个目录。(文件过滤器)

huangapple go评论72阅读模式
英文:

TreeContentProvider just getting every Directory of my path. (File Filter)

问题

我试图获取路径中的每个文件夹。现在我只获取文件夹,但是一旦出现其他文件,就会出现JavaNullPointerException。我知道这是因为 listFiles(),但我不知道如何以其他方式让它工作。有人可以帮助我吗?


    @Override
    public Object[] getElements(Object inputElement) {
        return File.listRoots();
    }
    
    @Override
    public Object getParent(Object element) {
        return ((File) element).getParentFile();
    }
    
    @Override
    public boolean hasChildren(Object element) {
        Object[] obj = getChildren(element);       
        return obj == null ? false : obj.length > 0;
        
    }
    
    @Override
    public Object[] getChildren(Object parentElement) {
        
        List<File> files = new ArrayList<>();
            for (File file : ((File) parentElement).listFiles()) {
                if (file.isDirectory()) {
                    files.add(file);
                }
            }
        return files.toArray();
    }

<details>
<summary>英文:</summary>

im trying to get every folder of my path.
Now Im getting just folders but as soon as some other file comes up it gives me a JavaNullPointerException.
I know its beacuse of ***listFiles()*** but I dont know how to let it work otherwise.
Can someone help me?

``

    @Override
    public Object[] getElements(Object inputElement) {
        return File.listRoots();
    }
    
    @Override
    public Object getParent(Object element) {
        return ((File) element).getParentFile();
    }
    
    @Override
    public boolean hasChildren(Object element) {
        Object[] obj = getChildren(element);       
        return obj == null ? false : obj.length &gt; 0;
        
    }
    
    @Override
    public Object[] getChildren(Object parentElement) {
        
        List&lt;File&gt; files = new ArrayList&lt;&gt;();
            for (File file : ((File) parentElement).listFiles()) {
                if (file.isDirectory()) {
                    files.add(file);
                }
            }
        return ((File) parentElement).listFiles();
    }


</details>


# 答案1
**得分**: 1

File.listFiles() 如果文件不是目录,则返回 null。这会导致您的异常。

要仅显示目录,请使用类似以下方式:
```java
@Override
public Object[] getChildren(Object parentElement) {
   File parentFile = (File)parentElement;

   if (!parentFile.isDirectory()) {
      return new Object[0];
   }

   return parentFile.listFiles(File::isDirectory);
}
英文:

File.listFiles() returns null if the file is not a directory. This is causing your exception.

To just show directories use something like:

@Override
public Object[] getChildren(Object parentElement) {
   File parentFile = (File)parentElement;

   if (!parentFile.isDirectory()) {
      return new Object[0];
   }

   return parentFile.listFiles(File::isDirectory);
}

huangapple
  • 本文由 发表于 2020年9月10日 23:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63832495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定