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

时间:2021-04-20 12:33:32
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 exception when 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 NullPointerException and show only non hidden and non system files in jTree?

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

3 个解决方案

#1


12  

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 File interface that tries to be system independent. You can use Command line commands though, if those exist.

至于系统文件,我认为这是Windows的概念,因此可能不支持试图独立于系统的文件接口。如果存在命令行命令,您可以使用命令行命令。

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中引入的。

#2


3  

If running under Windows, Java 7 introduced DosFileAttributes which 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());

#3


0  

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.

所以只需比较数组是否为null,然后设计递归,这样它就可以跳过列表()函数的数组为null的文件。

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创建一个窗口文件管理器的代码。

#1


12  

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 File interface that tries to be system independent. You can use Command line commands though, if those exist.

至于系统文件,我认为这是Windows的概念,因此可能不支持试图独立于系统的文件接口。如果存在命令行命令,您可以使用命令行命令。

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中引入的。

#2


3  

If running under Windows, Java 7 introduced DosFileAttributes which 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());

#3


0  

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.

所以只需比较数组是否为null,然后设计递归,这样它就可以跳过列表()函数的数组为null的文件。

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创建一个窗口文件管理器的代码。