以下实例演示了使用 File 类的 fileToChange.lastModified() 和 fileToChange setLastModified() 方法来修改文件最后的修改日期:
/* author by dba.cn Main.java */ import java.io.File; import java.util.Date; public class Main { public static void main(String[] args) throws Exception { File fileToChange = new File ("C:/myjavafile.txt"); fileToChange.createNewFile(); Date filetime = new Date (fileToChange.lastModified()); System.out.println(filetime.toString()); System.out.println (fileToChange.setLastModified (System.currentTimeMillis())); filetime = new Date (fileToChange.lastModified()); System.out.println(filetime.toString()); } }
以上代码运行输出结果为:
Sat Mar 21 22:00:48 CST 2015 true Fri Apr 10 11:09:19 CST 2015