Get File Attributes Using NIO
Get the attributes of a file using Java NIO.
Javadoc available at https://www.javatapas.com/docs/javatapas/io/GetFileAttributes.html
public static void getFileAttributes(Path path) throws IOException {
Map<String, String> map = new LinkedHashMap<>();
// add all basic file attributes
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
map.put("creationTime", attrs.creationTime().toString());
map.put("fileKey", attrs.fileKey().toString());
map.put("isDirectory", String.valueOf(attrs.isDirectory()));
map.put("isOther", String.valueOf(attrs.isOther()));
map.put("isRegularFile", String.valueOf(attrs.isRegularFile()));
map.put("isSymbolicLink", String.valueOf(attrs.isSymbolicLink()));
map.put("lastAccessTime", attrs.lastAccessTime().toString());
map.put("lastModifiedTime", attrs.lastModifiedTime().toString());
map.put("size", String.valueOf(attrs.size()));
try {
PosixFileAttributes attrsP = Files.readAttributes(path, PosixFileAttributes.class);
map.put("posixGroup", attrsP.group().toString());
map.put("posixOwner", attrsP.owner().toString());
map.put("posixPermissions", attrsP.permissions().toString());
}
catch (UnsupportedOperationException ex){ex.printStackTrace();}
try {
DosFileAttributes attrsD = Files.readAttributes(path, DosFileAttributes.class);
map.put("dosIsArchive", String.valueOf(attrsD.isArchive()));
map.put("dosIsHidden", String.valueOf(attrsD.isHidden()));
map.put("dosIsReadOnly", String.valueOf(attrsD.isReadOnly()));
map.put("dosIsSystem", String.valueOf(attrsD.isSystem()));
}
catch (UnsupportedOperationException ex){ex.printStackTrace();}
map.forEach((key, value) -> System.out.println(key + ": " + value));
}