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

后端 未结 3 1376
栀梦
栀梦 2021-01-20 16:51
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

3条回答
  •  清歌不尽
    2021-01-20 17:09

    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.

提交回复
热议问题