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

13,316

Solution 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.

Solution 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());
Share:
13,316
Akhilesh Dhar Dubey
Author by

Akhilesh Dhar Dubey

Akhilesh is a Software Engineer with over 6 years of experience in Android app development. He is a hard-working, quick-learner and innovative developer. He has experience in a variety of applications including Video OTT & TV Everywhere, Music, E-Commerce, Automobiles, and Banking, as well as extensive experience in Java. He loves to code, explore new technologies, keep learning every time and above all, spend quality time learning and sharing stuff with friends and the Stack Overflow family. He is a self-motivated and techno freak guy who loves to step up to new challenges. He is also passionate about code optimisation, best practices, and best user experience. He loves to play Table Tennis when not sitting in front of his laptop.

Updated on July 01, 2022

Comments

  • Akhilesh Dhar Dubey
    Akhilesh Dhar Dubey almost 2 years
    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?

    • Akhilesh Dhar Dubey
      Akhilesh Dhar Dubey about 11 years
      Is there any method provide File class which list only non-system and non-hidden file ?