Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the last modified date of a file in Java

I'm making a basic file browser and want to get the last modified date of each file in a directory. How might I do this? I already have the name and type of each file (all stored in an array), but need the last modified date, too.

like image 551
Ky. Avatar asked Dec 06 '10 04:12

Ky.


People also ask

How do you get the last modified date of all files in a folder in Java?

You can get the last modified time of a File using File. lastModified() . To list all of the files in a directory, use File. listFiles() .

How can I tell when Java was last updated?

How to find the last modified date of a file in Java. Before Java 7 e.g. in JDK 1.6, You can use the lastModified() method of the File class to get the last updated time. Just create a File object by passing the path as String to File class and call the last modified method as shown below: File source = new File(".

How do I change the last modified date of a file in Java?

Use parse(String date) method of the SimpleDateFormat class to create a new Date object with the date value of the String . Use File. setLastModified(Date. getTime()) method to set the new “Last Modified” date of the file.

How do you check if a file has been modified in Java?

To really know if the file has changed, you'd need to store a hash of the files (md5sum, sha256, etc) and for the comparison re-hash and compare the values of the 2 hashes.


1 Answers

As in the javadocs for java.io.File:

new File("/path/to/file").lastModified()

like image 103
icyrock.com Avatar answered Sep 21 '22 17:09

icyrock.com