Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thinks C:\ drive is hidden?

I think it's better if I explain the situation first.

Situation

I'm writing a bit of software that filters through a Set of Files.

The filter is the following: If the file is NOT hidden, then add it it to the new Set.

The problem is that the current behaviour of File.isHidden() is as follows:

File f = new File("C:\Documents and Settings\Administrator\Local Settings\Temp\REG28E.tmp");
System.out.println(f.isHidden());

The program will output false.

The file itself (REG28E.tmp) is NOT actually hidden. Rather, a certain folder in the path to it is hidden (Local Settings).

I want to create a bit of code that will check:

If, somewhere along the path to the file is hidden, then the file is marked as hidden.

I've come up with the following solution to check the file's path recursively:

boolean hidden = file.isHidden();
File parentFile = file.getParentFile();
while ((hidden == false) || (parentFile != null)) {
    hidden = parentFile.isHidden();
    parentFile = parentFile.getParentFile();
}

if (!hidden) {
    acceptedFileList.add(file);
}

The Problem

And finally, we come to the problem. I can never get to the stage where I'm actually adding files. The problem is that Java thinks that the C:\ drive itself is hidden!

What can I do instead?

like image 609
Redandwhite Avatar asked Nov 16 '25 11:11

Redandwhite


1 Answers

The solution involved adding a manual check to see whether the parentFile object is a root directory or not.

while ((hidden == false) && (parentFile != null)) {
    // added the IF statement below:
    if (FileSystemUtils.isRoot(parentFile)) {
        hidden = false;
        break;
    }

    hidden = parentFile.isHidden();
    parentFile = parentFile.getParentFile();
}


Checking if Root directory

The above code makes use of a FileSystemUtils class. It contains the following method:

public static boolean isRoot(File file) {
    File[] roots = File.listRoots();

    for (File root : roots) {
        if (file.equals(root)) {
        return true;
        }
    }

    return false;
}

Thanks to all who chimed in...

like image 167
Redandwhite Avatar answered Nov 18 '25 06:11

Redandwhite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!