Home » Java programming language

Java File Class long lastModified() method with Example

Java File Class long lastModified() method: Here, we are going to learn about the long lastModified() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 15, 2019

File Class long lastModified()

  • This method is available in package java.io.File.lastModified().
  • This method is used to return the time when the file is last modified.
  • In other words, this method indicates the work by its name it returns the last modification time of the file and time will be measurable in milliseconds.
  • The return type of this method is long so it returns the last modified time of the file and else returns 0L if the file does not exist or an exception occurs.

Syntax:

    long lastModified(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is long, it returns the time in milliseconds else return 0L if any exception occurs or file does not exist.

Java program to demonstrate example of lastModified() method

import java.io.*;

public class FileLastModifiedTime {
    public static void main(String[] args) {
        try {
            // Create a file object file1
            File file1 = new File("E:\\Programs\\myjava.txt");

            // Create a file object file2
            File file2 = new File("C:\\Users\\computerclinic\\OneDrive\\Articles\\myjava1.txt");

            // By using lastModified() method return the time 
            // when the file1 is last modified.
            System.out.println("The Last Modification Time Of File1 is : " + file1.lastModified());

            // By using lastModified() method return 0L because 
            // the file2 does not exists.
            System.out.println("The Last Modification Time Of File2 is : " + file2.lastModified());
        } catch (Exception e) {
            System.err.println("An error occurred");
            e.printStackTrace();
        }
    }
}

Output

E:\Programs>javac FileLastModifiedTime.java

E:\Programs>java FileLastModifiedTime
The Last Modification Time Of File1 is : 1563132418281
The Last Modification Time Of File2 is : 0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.