集册 Java实例教程 获取和设置文件或目录的修改时间

获取和设置文件或目录的修改时间

欢马劈雪     最近更新时间:2020-01-02 10:19:05

508
获取和设置文件或目录的修改时间

import java.io.File;
/*
NowJava.com - 时  代  Java 提 供
*/


public class Main {

  public void m() throws Exception {

    File file = new File("filename");

    // Get the last modified time

    long modifiedTime = file.lastModified();

    // 0L is returned if the file does not exist


    // Set the last modified time

    long newModifiedTime = System.currentTimeMillis();

    boolean success = file.setLastModified(newModifiedTime);

    if (!success) {

      // operation failed.

    }

  }

}