I think it's better if I explain the situation first.
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);
}
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?
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();
}
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With