Java IO Tutorial - Java File.lastModified()








Syntax

File.lastModified() has the following syntax.

public long lastModified()

Example

In the following code shows how to use File.lastModified() method.

//w w  w. j a va 2  s  . c o  m
import java.io.File;
import java.util.Date;

public class Main {
  public static void main(String[] args) {

    File f = new File("c:/test.txt");

    boolean bool = f.exists();

    // if path exists
    if (bool) {
      // returns the time file was last modified
      long millisec = f.lastModified();

      // date and time
      Date dt = new Date(millisec);

      // path
      String path = f.getPath();

      System.out.print(path + " last modified at: " + dt);
    }

  }
}