JTree显示驱动器根本不显示

时间:2022-11-22 12:37:44

My task is to show a tree of all directories/files of a PC drive, I have a class DirectoryNode that extends DefaultMutableTreeNode with File field directoryPath. I build nodes recursively:

我的任务是显示PC驱动器的所有目录/文件的树,我有一个DirectoryNode类,它使用File字段directoryPath扩展DefaultMutableTreeNode。我递归地构建节点:

public void buildDirectoryTree(){
    if(!directoryPath.isDirectory()){
            return;
    }

    for(File f : directoryPath.listFiles()){
        if(f.isHidden() || !f.exists()) continue;
        DirectoryNode newChild = new DirectoryNode(f);
        add(newChild);
        newChild.buildDirectoryTree();
    }
}

It works fine for concrete directories, but when I try to use it for whole drive, or some large directories, JTree with this node does not show up at all I think it encounters a problem with specific directories. I've add exists and is Hidden checks to skip this problem roots, but it didn't help.

它适用于具体目录,但是当我尝试将它用于整个驱动器或一些大型目录时,具有此节点的JTree根本不会显示,我认为它遇到了特定目录的问题。我添加了存在并且是隐藏的检查以跳过此问题根,但它没有帮助。

In addition, exists, isHidden and isDirectory return false for some of my valid directories directories (I am using Windows 10).

另外,对于我的一些有效目录目录(我使用的是Windows 10),exists,isHidden和isDirectory返回false。

1 个解决方案

#1


1  

File.listFiles() is one of those ancient methods that violates Java's convention/good practice to never return null from a method that returns an array. So you have to check for null.

File.listFiles()是违反Java的约定/良好实践的古老方法之一,永远不会从返回数组的方法返回null。所以你必须检查null。

From the docs:

来自文档:

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

一组抽象路径名,表示此抽象路径名表示的目录中的文件和目录。如果目录为空,则数组将为空。如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。

I have changed your code to make it a little safer. If it's called from the EDT, you might want to add some log message or the like instead of throwing the exception into nirvana.

我已经更改了您的代码,使其更安全一些。如果从EDT调用它,您可能想要添加一些日志消息等,而不是将异常抛入Nirvana。

public void buildDirectoryTree() throws IOException {
    if (!directoryPath.isDirectory()) {
            return;
    }
    final File[] files = directoryPath.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isHidden() || !f.exists()) continue;
            DirectoryNode newChild = new DirectoryNode(f);
            add(newChild);
            newChild.buildDirectoryTree();
        }
    } else {
        throw new IOException("Failed to list files for " + directoryPath);
    }
}

As others have pointed out, there are more modern APIs and they have been introduced for good reasons. I recommend to read up on NIO2 and the Path APIs for better solutions.

正如其他人所指出的那样,有更多的现代API,并且有充分的理由引入它们。我建议您阅读NIO2和Path API以获得更好的解决方案。

#1


1  

File.listFiles() is one of those ancient methods that violates Java's convention/good practice to never return null from a method that returns an array. So you have to check for null.

File.listFiles()是违反Java的约定/良好实践的古老方法之一,永远不会从返回数组的方法返回null。所以你必须检查null。

From the docs:

来自文档:

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

一组抽象路径名,表示此抽象路径名表示的目录中的文件和目录。如果目录为空,则数组将为空。如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。

I have changed your code to make it a little safer. If it's called from the EDT, you might want to add some log message or the like instead of throwing the exception into nirvana.

我已经更改了您的代码,使其更安全一些。如果从EDT调用它,您可能想要添加一些日志消息等,而不是将异常抛入Nirvana。

public void buildDirectoryTree() throws IOException {
    if (!directoryPath.isDirectory()) {
            return;
    }
    final File[] files = directoryPath.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isHidden() || !f.exists()) continue;
            DirectoryNode newChild = new DirectoryNode(f);
            add(newChild);
            newChild.buildDirectoryTree();
        }
    } else {
        throw new IOException("Failed to list files for " + directoryPath);
    }
}

As others have pointed out, there are more modern APIs and they have been introduced for good reasons. I recommend to read up on NIO2 and the Path APIs for better solutions.

正如其他人所指出的那样,有更多的现代API,并且有充分的理由引入它们。我建议您阅读NIO2和Path API以获得更好的解决方案。