Java.io.File.lastModified() Method

Advertisements


Description

The java.io.File.lastModified() returns the last modified time of the file denoted by this abstract pathname.

Declaration

Following is the declaration for java.io.File.lastModified() method:

public long lastModified()

Parameters

  • NA

Return Value

The method returns the last modified time of the file denoted by this abstract pathname.

Exception

  • SecurityException -- If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Example

The following example shows the usage of java.io.File.lastModified() method.

package com.tutorialspoint;

import java.io.File;
import java.util.Date;

public class FileDemo {
   public static void main(String[] args) {
      
      File f = null;
      String path;
      long millisec;
      boolean bool = false;
      
      try{      
         // create new file
         f = new File("c:/test.txt");
         
         // true if the file path is a file, else false
         bool = f.exists();
         
         // if path exists
         if(bool)
         {
            // returns the time file was last modified
            millisec = f.lastModified();
            
            // date and time
            Date dt = new Date(millisec);
            
            // path
            path = f.getPath();
            
            // print
            System.out.print(path+" last modified at: "+dt);
         }
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

c:\test.txt last modified at: Sat Jun 09 21:36:21 IST 2012



Advertisements
Advertisements