java中lastmodified_Java File lastModified用法及代码示例

时间:2025-03-21 09:12:12

lastModified()函数是Java中File类的一部分。该函数返回该抽象路径名最后一次修改所表示的时间。该函数返回以毫秒为单位的long值,表示文件上次修改的时间;如果文件不存在或发生异常,则返回0L。

函数签名:

public long lastModified()

用法:

long valr = ();

参数:此方法不接受任何参数。

返回类型此函数返回表示文件上次修改时间的long类型,如果文件不存在,则返回0L。

异常:如果对文件的写访问被拒绝,则此方法将引发安全性异常

以下示例程序旨在说明lastModified()函数的用法:

范例1:文件“F:\\”是F:目录中的现有文件。

// Java program to demonstrate

// lastModified() method of File Class

import .*;

public class solution {

public static void main(String args[])

{

// Get the file

File f = new File("F:\\");

// Get the last modified time of the file

("Last modified: "

+ ());

}

}

输出:

Last modified: 1542654265978

范例2:文件“F:\\”不存在

// Java program to demonstrate

// lastModified() method of File Class

import .*;

public class solution {

public static void main(String args[])

{

// Get the file

File f = new File("F:\\");

// Get the last modified time of the file

("Last modified: "

+ ());

}

}

输出:

Last modified: 0

注意:程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的路径。