例子
以下示例显示java.io.File.lastModified()方法的用法。
package com.jc2182;
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();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
c:\test.txt last modified at: Sat Jun 09 21:36:21 IST 2012