Koltin Files.getAttribute()

The Files class in JDK has a getAttribute() that accepts a Path object and returns a specified attribute.

import java.nio.file.Files
import java.nio.file.Paths

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 ->
            //getAttribute() asserts non-null, so we can add compiler null checks by declaring a nullable type if desired
            val creationTime = Files.getAttribute(it, "creationTime")
            val lastModified = Files.getAttribute(it, "lastModifiedTime")
            val size = Files.getAttribute(it, "size")
            val dir = Files.getAttribute(it, "isDirectory")

            println("${it.fileName}")
            println("Creation Time => $creationTime")
            println("Last Modified => $lastModified")
            println("Size => $size")
            println("Directory => $dir")
            println()
        })
    } else {
        println("Please enter a path to a directory")
    }
}

The demonstration of Files.getAttribute() is found on lines 15 – 18. In each case, the getAttribute() method accepts a Path object, a string name of the attribute, and optional LinkOptions. The value returned is of type any, and the return type is assert not null (!!) operator. Since the names of the attributes is a string, there is the possibility that an exception could get thrown also.

The getAttribute() method can be convient, but it has short comings. Since it is a Java class, Kotlin interprets it as returning assert non-null. This may or may not be a problem. The java-doc makes no mention of a null return type, so it’s most likely safe to assume non-null types. However, it compiler checks are wanted or desirable, then we can declare nullable types.

More troubling is the fact that Strings are used for the attribute types. We have no protections against a typo such as “sizes” when we meant “size”. We will get a runtime exception in the event that attribute doesn’t exist. Alway check the java document prior to using the getAttribute() method to make sure the attribute is available first, keeping in mind that some attributes are platform specific.

Note that there is also a readAttributes() method, found in an upcoming post, that offers much better type safety than getAttribute(). However, the getAttribute() is useful in the case that we wish to check only on specific attribute (such as size), without having to use additional objects.

References

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#getAttribute(java.nio.file.Path,%20java.lang.String,%20java.nio.file.LinkOption…)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s