java - Why doesn't Files.isHidden() working correctly? -
i'm messing around java nio , reason can't files.ishidden() return correct boolean value. program checks see if directory hidden if hidden make visible , if not hidden make hidden. have:
path start = filesystems.getdefault().getpath("e:/documents/somedirectory"); try { if (files.ishidden(start)){ system.out.println("dir hidden."); files.setattribute(start, "dos:hidden", false); } else { system.out.println("dir not hidden. hiding."); files.setattribute(start, "dos:hidden", true); } } catch (ioexception e) { e.printstacktrace(); }
it keeps returning false , hiding directory despite directory being hidden. following code works fine using old file class w/ path class.
path start = filesystems.getdefault().getpath("e:/documents/somedirectory"); file file = new file("e:/documents/somedirectory"); try { if (file.ishidden()){ system.out.println("dir hidden."); files.setattribute(start, "dos:hidden", false); } else { system.out.println("dir not hidden. hiding."); files.setattribute(start, "dos:hidden", true); } } catch (ioexception e) { e.printstacktrace(); }
as pointed out in comments, documentation of files.ishidden
states:
the exact definition of hidden platform or provider dependent. […] on windows file considered hidden if isn't directory , dos hidden attribute set.
while last cited sentence explains while doesn’t return expected value directory on windows, want emphasize first sentence. using method burdened platform/provider specific semantics, while want do, toggle particular, platform specific flag.
in case, should that, elides conditionals of code:
path start=paths.get("e:/documents/somedirectory"); boolean ishidden=(boolean)files.getattribute(start, "dos:hidden"); system.out.println("dir "+(ishidden? "hidden. showing.": "not hidden. hiding")); files.setattribute(start, "dos:hidden", !ishidden);
note convenience method paths.get(…)
filesystems.getdefault().getpath(…)
.
Comments
Post a Comment