The Files.readAttributes() method comes from JDK and is used to return meta-data about a particular file.
import java.nio.file.Files import java.nio.file.Paths import java.nio.file.attribute.BasicFileAttributes fun main(args : Array<String>){ val path = if(args.isEmpty()){ Paths.get(System.getProperty("user.dir")) } else { Paths.get(args[0]) } if(Files.isDirectory(path)){ Files.list(path).forEach({ it -> val attrs = Files.readAttributes(it, BasicFileAttributes::class.java) println("${it.fileName}") println("Size => ${attrs.size()}") println("Directory => ${attrs.isDirectory}") println("Regular File => ${attrs.isRegularFile}") println("Link => ${attrs.isSymbolicLink}") println("Last Accessed => ${attrs.lastAccessTime()}") println("Last Modified => ${attrs.lastModifiedTime()}") println() }) } else { println("Enter a path to a directory") } }
The readAttributes() call is made on line 16. The readAttributes() method takes a Path object and then a Java class object of BasicFileAttribures or one of its subinterfaces, DosFileAttributes or PosixFileAttributes. Depending on the Java class specified, the method will return either BasicFileAttributes, DosFileAttributes, or PosixFileAttributes. BasicFileAttributes is used in this example because it is portable across all systems, while the other two interfaces are specific to their respective platforms.
BasicFileAttributes provides many commonly used file attributes in a type safe fashion. We can access common attributes such as when the file was created, modified, or last accessed. The interface has boolean attributes to check if a Path is a directory or symbolic links. We can even check the size of the file with the BasicFileAttributes.
The DosFileAttributes interface has properties specific to DOS based platforms (i.e., Windows). We can check if a file is a system file, hidden file, read only, or archived. The PosixFileAttributes interface is used on unix based platforms such as Mac OS X or Linux. It contains properties file permissions, user groups, and the file owner.
References
https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html
https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAttributes(java.nio.file.Path,%20java.lang.Class,%20java.nio.file.LinkOption…)