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(); }
info.setLastModified(attrs.lastModifiedTime().toMillis()); info.setLength(attrs.size()); 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_SYMLINK, true); info.setStringAttribute(EFS.ATTRIBUTE_LINK_TARGET, Files.readSymbolicLink(path).toString()); } else { info.setDirectory(attrs.isDirectory());
linkedHashMap.put("File Size", readableFileSize(dosFileAttr.size())); linkedHashMap.put("Creation Time", getDateTime(dosFileAttr.creationTime())); linkedHashMap.put("Last Accessed Time", getDateTime(dosFileAttr.lastAccessTime())); linkedHashMap.put("Last Modified Time", getDateTime(dosFileAttr.lastModifiedTime())); linkedHashMap.put("Is Directory?", dosFileAttr.isDirectory() ? "True" : "False"); linkedHashMap.put("Is Regular File?", dosFileAttr.isRegularFile() ? "True" : "False"); 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");
DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class); if (attr.isArchive()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_ARCHIVE .getValue(); if (attr.isDirectory()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_DIRECTORY .getValue(); if (attr.isHidden()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_HIDDEN .getValue(); if (attr.isReadOnly()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_READONLY .getValue(); if (attr.isRegularFile()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_NORMAL .getValue(); if (attr.isSymbolicLink()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_REPARSE_POINT .getValue(); if (attr.isSystem()) fileAttribute |= FileAttributeFlags.FILE_ATTRIBUTE_SYSTEM .getValue(); creationTime = millisToFiletime(attr.creationTime().toMillis()); lastAccessTime = millisToFiletime(mf.getLastAccessed());
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); } } }
static int attributes_to_mode(DosFileAttributes attr) { int m = 0; if (attr.isDirectory()) { m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ } else { m |= _S_IFREG; } if (attr.isReadOnly()) { m |= 0444; } else { m |= 0666; } return m; }
private boolean isReadOnlyFile(Path file) { if(Platform.isWindows()) { if (!file.toFile().canWrite()) { DosFileAttributes dosAttr; try { dosAttr = Files.readAttributes(file, DosFileAttributes.class); if(dosAttr.isReadOnly()) { return true; } } catch (IOException e) { } } } return false; }
// import as necessary from java.nio.file and java.io DosFileAttributeView view = input.getFileAttributeView(DosFileAttributeView.class); //Check if the system supports this view if (view != null) { DosFileAttributes attributes = view.readAttributes(); // skip any file already marked as an archive if (!attributes.isArchive()) { myObject.process(input) attributes.setArchive(true) } }
public static PyStatResult fromDosFileAttributes(int mode, DosFileAttributes stat) { return new PyStatResult( Py.newInteger(mode), Py.newInteger(0), Py.newLong(0), Py.newInteger(0), Py.newInteger(0), Py.newInteger(0), Py.newInteger(stat.size()), Py.newFloat(fromFileTime(stat.lastAccessTime())), Py.newFloat(fromFileTime(stat.lastModifiedTime())), Py.newFloat(fromFileTime(stat.creationTime()))); }
@Override public Object readAttribute(AttributeReadContext context) throws IOException { return context.getDosFileAttributes().isArchive(); }
Path srcFile = Paths.get("test"); DosFileAttributes dfa = Files.readAttributes(srcFile, DosFileAttributes.class); System.out.println("isSystem? " + dfa.isSystem());
@Override public PyObject __call__(PyObject path) { Path absolutePath = absolutePath(path); try { DosFileAttributes attributes = Files.readAttributes(absolutePath, DosFileAttributes.class); if (!attributes.isDirectory()) { String pathStr = path.toString(); if (pathStr.endsWith(File.separator) || pathStr.endsWith("/")) { throw Py.OSError(Errno.ENOTDIR, path); } } int mode = attributes_to_mode(attributes); String extension = com.google.common.io.Files.getFileExtension(absolutePath.toString()); if (extension.equals("bat") || extension.equals("cmd") || extension.equals("exe") || extension.equals("com")) { mode |= 0111; } return PyStatResult.fromDosFileAttributes(mode, attributes); } catch (NoSuchFileException ex) { throw Py.OSError(Errno.ENOENT, path); } catch (IOException ioe) { throw Py.OSError(Errno.EBADF, path); } catch (SecurityException ex) { throw Py.OSError(Errno.EACCES, path); } } }
info.setLastModified(attrs.lastModifiedTime().toMillis()); info.setLength(attrs.size()); 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_SYMLINK, true); info.setStringAttribute(EFS.ATTRIBUTE_LINK_TARGET, Files.readSymbolicLink(path).toString()); } else { info.setDirectory(attrs.isDirectory());
@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("."); }
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()); } });
public static boolean canWrite(String path) { if (SystemInfo.isWindows) { try { DosFileAttributes attributes = Files.readAttributes(Paths.get(path), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return attributes.isDirectory() || !attributes.isReadOnly(); } catch (IOException e) { ExceptionUtil.throwRuntime(e); } } return new File(path).canWrite(); }
@Override public Object readAttribute(AttributeReadContext context) throws IOException { return context.getDosFileAttributes().isReadOnly(); }
@Override public Object readAttribute(AttributeReadContext context) throws IOException { return context.getDosFileAttributes().isSystem(); }
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); }