Need your Java application to get the last modified time on a file? Well that's pretty easy.
And in this guide, I'll show you how to grab that info with Java NIO.
Let's get started.
Back to Basics
For today's guide, I'm going to show you a class called BasicFileAttributes.
It's the class you'll use when you want to grab info about the file as opposed to the contents of the file.
One piece of info you can get with BasicFileAttributes is the last modified date and time. Since that's why you're here, you'll find that class especially important.
Now let's take a look at some code that uses it:
String fileName = "/etc/careydevelopment/ecosystem.properties"; Path file = Paths.get(fileName); BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); FileTime fileTime = attr.lastModifiedTime(); System.err.println("Last modified time: " + fileTime);
The first line creates a String object that points to the file on the operating system. Here, I'm using a properties file in /etc/careydvelopment.
But you'll need to change that line. Set fileName to a file path you'd like to test with.
The next line instantiates a Path object. That's the Java NIO aspect of this guide.
Note that the Path object gets created from the String set in the previous line.
Now that the code has a handle on the file, it can read the attributes. That's what's happening in the third and fourth lines of the code.
And yeah, the way that gets instantiated is kind of... weird, isn't it?
It uses the static readAttributes() method from the Files class. The first parameter is the Path object and the second parameter defines the type of object that gets instantiated.
You might be asking yourself, which other kinds of attributes types are there besides BasicFileAttributes?
The only other two documented sub-interfaces are DosFileAttributes and PosixFileAttributes. But you don't need to worry about those for this guide.
The next line gets the last modified time from the BasicFileAttributes object. That seems pretty straightforward but you might not recognize the type of object that gets instantiated: FileTime.
What the Heck is FileTime?
There aren't too many classes defined in the Java programming language with a more appropriate name than FileTime.
It's used to represent the last time that the file was accessed or modified. It's also used to represent when the file was created.
Here, you're going to use it to determine the last time that the file was modified. That's what attr.lastModifiedTime() will do for you.
Finally that last line of code above prints out the value of the FileTime object in pretty red letters. Fortunately, the toString() override in that class delivers a really great readout.
It looks like this:
Last modified time: 2021-03-04T18:50:21.268968Z
If you want to format that to look like something else... well, that's a subject for a different article.
Wrapping It Up
Now you know how to get the last modified time of a file using Java NIO.
By the way: you could just as easily get the last accessed time with attr.lastAccessTime() in the code above. So give that a shot if you ever need it.
Photo by KoolShooters from Pexels