java 如何在jtree中仅列出非隐藏和非系统文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15646358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:24:13  来源:igfitidea点击:

How to list only non hidden and non system file in jtree

javaswingfile-iojtreefilefilter

提问by Akhilesh Dhar Dubey

File f=new File("C:/");
File fList[] = f.listFiles();

When i use this it list all system file as well as hidden files.

当我使用它时,它会列出所有系统文件以及隐藏文件。

and this cause null pointer exceptionwhen i use it to show in jTree like this:

当我使用它在 jTree 中显示时,这会导致空指针异常,如下所示:

 public void getList(DefaultMutableTreeNode node, File f) {
 if(f.isDirectory()) {
     DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
     node.add(child);
     File fList[] = f.listFiles();
     for(int i = 0; i  < fList.length; i++)
         getList(child, fList[i]);
     }
}

What should i do so that it do not give NullPointerExceptionand show only non hidden and non system filesin jTree?

我应该怎么做才能不给NullPointerException并在 jTree 中只显示非隐藏和非系统文件

回答by Sotirios Delimanolis

Do this for hidden files:

对隐藏文件执行此操作:

File root = new File(yourDirectory);
File[] files = root.listFiles(new FileFilter() {
    @Override
    public boolean accept(File file) {
        return !file.isHidden();
    }
});

This will not return hidden files.

这不会返回隐藏文件。

As for system files, I believe that is a Windows concept and therefore might not be supported by Fileinterface that tries to be system independent. You can use Command line commands though, if those exist.

至于系统文件,我相信这是一个 Windows 概念,因此可能不受File试图独立于系统的界面的支持。不过,您可以使用命令行命令(如果存在)。

Or use what @Reimeus had in his answer.

或者使用@Reimeus 在他的回答中的内容。

Possibly like

可能喜欢

    File root = new File("C:\");

    File[] files = root.listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            Path path = Paths.get(file.getAbsolutePath());
            DosFileAttributes dfa;
            try {
                dfa = Files.readAttributes(path, DosFileAttributes.class);
            } catch (IOException e) {
                // bad practice
                return false;
            }
            return (!dfa.isHidden() && !dfa.isSystem());
        }
    });

DosFileAttributes was introduced in Java 7.

DosFileAttributes 是在 Java 7 中引入的。

回答by Reimeus

If running under Windows, Java 7 introduced DosFileAttributeswhich enables system and hidden files to be filtered. This can be used in conjunction with a FileFilter

如果在 Windows 下运行,Java 7 引入了DosFileAttributes,它可以过滤系统和隐藏文件。这可以结合使用FileFilter

Path srcFile = Paths.get("myDirectory");
DosFileAttributes dfa = Files.readAttributes(srcFile, DosFileAttributes.class);
System.out.println("System File? " + dfa.isSystem());
System.out.println("Hidden File? " + dfa.isHidden());

回答by Pranjali

If you are trying to list all files in C:/please keep in mind that there are other files also which are neither hidden nor system files, but that still won't open because they require special privileges/permissions. So:

如果您尝试列出所有文件,C:/请记住还有其他文件既不是隐藏文件也不是系统文件,但仍然无法打开,因为它们需要特殊的特权/权限。所以:

String[] files = file.list();

if (files!=null) {
    for (String f : files) open(f);
}

So just compare if the array is null or not and design your recursion in such a way that it just skips those files whose array for the list()function is null.

因此,只需比较数组是否为空,并以这样的方式设计您的递归,即它只是跳过那些list()函数数组为空的文件。

private void nodes(DefaultMutableTreeNode top, File f) throws IOException {

if (f.isDirectory()) {
    File[] listFiles = f.listFiles();

    if (listFiles != null) {
        DefaultMutableTreeNode b1[] = new DefaultMutableTreeNode[listFiles.length];
        for (int i = 0; i < b1.length; i++) {
            b1[i] = new DefaultMutableTreeNode(listFiles[i].toString());
            top.add(b1[i]);
            File g = new File(b1[i].toString());
            nodes(b1[i], g);
        }
    }
}

Here is the code I used to create a window file explorer using jtree.

这是我用来使用 jtree 创建窗口文件资源管理器的代码。