private boolean isHidden(Path path) throws IOException { if (SystemUtils.IS_OS_WINDOWS) { try { DosFileAttributes dosFileAttributes = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return dosFileAttributes.isHidden(); } catch (UnsupportedOperationException e) { return path.toFile().isHidden(); } } else { return Files.isHidden(path); } } }
public static String toString(DosFileAttributes dosFileAttributes) { StringBuilder sb = new StringBuilder(); sb.append(dosFileAttributes.isReadOnly() ? "r" : "-"); sb.append(dosFileAttributes.isHidden() ? "h" : "-"); sb.append(dosFileAttributes.isArchive() ? "a" : "-"); sb.append(dosFileAttributes.isSystem() ? "s" : "-"); return sb.toString(); }
Path file = ...; try { DosFileAttributes attr = Attributes.readDosFileAttributes(file); System.out.println("isReadOnly is " + attr.isReadOnly()); System.out.println("isHidden is " + attr.isHidden()); System.out.println("isArchive is " + attr.isArchive()); System.out.println("isSystem is " + attr.isSystem()); } catch (IOException x) { System.err.println("DOS file attributes not supported:" + x); }
@Override public boolean isHidden(Path path) throws IOException { // TODO(cgdecker): This should probably be configurable, but this seems fine for now /* * If the DOS view is supported, use the Windows isHidden method (check the dos:hidden * attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with * "."). */ JimfsPath checkedPath = checkPath(path); FileSystemView view = getDefaultView(checkedPath); if (getFileStore(path).supportsFileAttributeView("dos")) { return view .readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS) .isHidden(); } return path.getNameCount() > 0 && path.getFileName().toString().startsWith("."); }
Files.setAttribute(filePath, "dos:hidden", dosAttrs.isHidden()); Files.setAttribute(filePath, "dos:archive", dosAttrs.isArchive()); Files.setAttribute(filePath, "dos:system", dosAttrs.isSystem());
DosFileAttributes dosAttrsRHAS = FileUtil.dosAttrsFromString("rhas"); assertTrue(dosAttrsRHAS.isReadOnly()); assertTrue(dosAttrsRHAS.isHidden()); assertTrue(dosAttrsRHAS.isArchive()); assertTrue(dosAttrsRHAS.isSystem()); assertTrue(dosAttrsRHA.isHidden()); assertTrue(dosAttrsRHA.isArchive()); assertFalse(dosAttrsRHA.isSystem()); assertTrue(dosAttrsRH.isHidden()); assertFalse(dosAttrsRH.isArchive()); assertFalse(dosAttrsRH.isSystem()); assertFalse(dosAttrsR.isHidden()); assertFalse(dosAttrsR.isArchive()); assertFalse(dosAttrsR.isSystem()); assertFalse(dosAttrsNone.isHidden()); assertFalse(dosAttrsNone.isArchive()); assertFalse(dosAttrsNone.isSystem()); assertTrue(dosAttrsH.isHidden()); assertFalse(dosAttrsH.isArchive()); assertFalse(dosAttrsH.isSystem()); assertFalse(dosAttrsCorrect.isHidden()); assertFalse(dosAttrsCorrect.isArchive()); assertFalse(dosAttrsCorrect.isSystem());
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());
private boolean isHidden(Path path) throws IOException { if (SystemUtils.IS_OS_WINDOWS) { try { DosFileAttributes dosFileAttributes = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return dosFileAttributes.isHidden(); } catch (UnsupportedOperationException e) { return path.toFile().isHidden(); } } else { return Files.isHidden(path); } } }
/** * Due to the way that windows handles hidden files vs. *nix * we use this method to determine if a file or folder is really hidden * @param path the file or folder to check if hidden * @return if the file or folder is hidden * @throws IOException if there is an error reading the file/folder */ public static boolean isHidden(final Path path) throws IOException{ //cause Files.isHidden() doesn't work properly for windows if the file is a directory if (System.getProperty("os.name").contains("Windows")){ return Files.readAttributes(path, DosFileAttributes.class).isHidden(); } return Files.isHidden(path); }
@Override public Object readAttribute(AttributeReadContext context) throws IOException { return context.getDosFileAttributes().isHidden(); }
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()); } });
boolean isHidden(AbstractPath abstractPath) throws IOException { if (this.supportedFileAttributeViews.contains(FileAttributeViews.POSIX)) { return this.accessFileReading(abstractPath, false, new MemoryEntryBlock<Boolean>(){ @Override public Boolean value(MemoryEntry entry) throws IOException { // Posix seems to check only the file name String originalName = entry.getOriginalName(); return !originalName.isEmpty() && originalName.charAt(0) == '.'; } }); } else if (this.supportedFileAttributeViews.contains(FileAttributeViews.DOS)) { return this.readAttributes(abstractPath, DosFileAttributes.class).isHidden(); } else { return false; } }
@Override public boolean isHidden(Path path) throws IOException { // TODO(cgdecker): This should probably be configurable, but this seems fine for now /* * If the DOS view is supported, use the Windows isHidden method (check the dos:hidden * attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with * "."). */ JimfsPath checkedPath = checkPath(path); FileSystemView view = getDefaultView(checkedPath); if (getFileStore(path).supportsFileAttributeView("dos")) { return view .readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS) .isHidden(); } return path.getNameCount() > 0 && path.getFileName().toString().startsWith("."); }
@Override public boolean isHidden( @Nullable Path path ) throws IOException { EightyPath ep = checkProvider( path ); AttributeProvider attributeProvider = ep._getFileSystem().getAttributeProvider(); if( attributeProvider.isViewSupported( DosFileAttributeView.class ) ) { return _nn( readAttributes( path, DosFileAttributes.class ) ).isHidden(); } @Nullable EightyPath fname = ep.getFileName(); return fname != null && fname.toString().startsWith( "." ); }
linkedHashMap.put("Is Symbolic Link?", dosFileAttr.isSymbolicLink() ? "True" : "False"); linkedHashMap.put("Is Archive?", dosFileAttr.isArchive() ? "True" : "False"); linkedHashMap.put("Is Hidden File?", dosFileAttr.isHidden() ? "True" : "False"); linkedHashMap.put("Is ReadOnly?", dosFileAttr.isReadOnly() ? "True" : "False"); linkedHashMap.put("Is System File?", dosFileAttr.isSystem() ? "True" : "False");
attr = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); boolean goHidden = attr.isHidden(); if (!hidden && goHidden) { Files.setAttribute(path, "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) { info.setDirectory(isDirectoryLink(attrs));
info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) { info.setDirectory(isDirectoryLink(attrs));
info.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attrs.isArchive()); info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attrs.isReadOnly()); info.setAttribute(EFS.ATTRIBUTE_HIDDEN, attrs.isHidden()); if (attrs.isSymbolicLink()) { info.setDirectory(isDirectoryLink(attrs));
fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_DIRECTORY .getValue(); if (attr.isHidden()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_HIDDEN .getValue();