long lastModified()
描述 (Description)
java.io.File.lastModified()返回此抽象路径名表示的文件的最后修改时间。
声明 (Declaration)
以下是java.io.File.lastModified()方法的声明 -
public long lastModified()
参数 (Parameters)
NA
返回值 (Return Value)
该方法返回此抽象路径名表示的文件的上次修改时间。
异常 (Exception)
SecurityException - 如果存在安全管理器且其SecurityManager.checkRead(java.lang.String)方法拒绝对文件的读访问权
例子 (Example)
以下示例显示了java.io.File.lastModified()方法的用法。
package com.iowiki;
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