Related Articles

Related Articles

Checking Last Modification of a File On the Server in Java
  • Last Updated : 17 Nov, 2020

In Java, we have different classes like File, URL which provides the functionality to read the Attributes of file like creation time, last access time, and last modified time.

Method 1(Using BasicFileAttributes)

This example uses java.nio.* to display the metadata of the file and other file attributes like creation time, last access time, and last modified time.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to get last modification time of the file
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
  
public class GFG {
    public static void main(String args[])
    {
        // storing the path of the file in the string
        String fileName = "C:/Users/elavi/Desktop/File.txt";
  
        // used exception handling in order to catch the
        // exception
        // which may occur if there is no such file is
        // present at specified location
        // or the path of the file provided by the user is
        // incorrect
        try {
            // getting the path of the file
            Path file = Paths.get(fileName);
            
            // reading the attributes of the file
            BasicFileAttributes attr = Files.readAttributes(
                file, BasicFileAttributes.class);
  
            // getting the last modification time of the
            // file
            System.out.println(
                "lastModifiedTime of File Is : "
                + attr.lastModifiedTime());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

chevron_right


Output: The output contains the date and time at which the last modification in the file was done.



last modification time of a file

Method 2(Using File.lastModified) 

For the legacy IO, one can use File.lastModified() method in order to get the last modified time of the file, this function returns the last modified time of the file as a long value which can be measured in milliseconds. We can use SimpleDataFormat class to make the returned result more readable. 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to get last modification time of the file
import java.io.File;
import java.text.SimpleDateFormat;
  
public class GFG_Article {
  
    public static void main(String[] args)
    {
        // path of the file
        String fileName = "C:/Users/elavi/Desktop/File.txt";
  
        File file = new File(fileName);
  
        // getting the last modified time of the file in the
        // raw format ie long value of milliseconds
        System.out.println("Before Format : "
                           + file.lastModified());
  
        // getting the last modified time of the file in
        // terms of time and date
        SimpleDateFormat sdf
            = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  
        System.out.println(
            "The Date and Time at which the file was last modified is "
            + sdf.format(file.lastModified()));
    }
}

chevron_right


Output: The output contains the date and time at which the last modification in the file was done.

last modification time of a file

Method 3(Using URL class) 

To check the last modification time of the uploaded file on the server, we can make use of URL class, and then can fetch the last modification time of the file.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to get last modification time of the file
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Date;
  
public class GFG {
    public static void main(String[] args) throws Exception
    {
          // resource url
        URL u = new URL(
        URLConnection uc = u.openConnection();
        uc.setUseCaches(false);
        
        // getting the last modified time of the file
        // uploaded on the server
        long timestamp = uc.getLastModified();
        System.out.println(
            "The last modification time of java.bmp is :"
            + timestamp);
    }
}

chevron_right


Output

last modification time of a file

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :