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

烈酒焚心 提交于 2019-12-02 01:25:44

问题


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:

 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?


回答1:


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.

Or use what @Reimeus had in his answer.

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.




回答2:


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

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:


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:

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.

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.



来源:https://stackoverflow.com/questions/15646358/how-to-list-only-non-hidden-and-non-system-file-in-jtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!