unknown_seagull
unknown_seagull

Reputation: 91

How to show only non-hidden folders programmatically

So I have this simple method that helps list all of the files in a directory (hidden and non-hidden) and I wish to edit it to display only non hidden files.

 public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File file = new File(DirectoryPath);

        //You create an array list
        //The public final field length, which contains the number of components of the array
        file.mkdirs();
        File[] files = file.listFiles();
        //list all of the files in the array
        if (files.length == 0)
            //if there are no files, return null
            return null;
        else {
            //if the number of files is greater than 0, add the files and their names
            for (int i = 0; i < files.length; i++)
                MyFiles.add(files[i].getName());
        }

        return MyFiles;

How can I modify the above code to only display non hidden files?

By the way, inserting a . in front of a file name hides the file.

Thank you.

Upvotes: 0

Views: 1562

Answers (1)

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

You should be able to make use of the isHidden(). Refer to the docs here.

//if the number of files is greater than 0, add the files and their names
        for (int i = 0; i < files.length; i++) {
            if(!files[i].isHidden()) 
                MyFiles.add(files[i].getName()); // Add non hidden files
        }

Upvotes: 4

Related Questions