lastModified()函数是Java中File类的一部分。该函数返回该抽象路径名最后一次修改所表示的时间。该函数返回以毫秒为单位的long值,表示文件上次修改的时间;如果文件不存在或发生异常,则返回0L。
函数签名:
public long lastModified()
用法:
long valr = file.lastModified();
参数:此方法不接受任何参数。
返回类型此函数返回表示文件上次修改时间的long类型,如果文件不存在,则返回0L。
异常:如果对文件的写访问被拒绝,则此方法将引发安全性异常
以下示例程序旨在说明lastModified()函数的用法:
范例1:文件“F:\\program.txt”是F:目录中的现有文件。
// Java program to demonstrate
// lastModified() method of File Class
import java.io.*;
public class solution {
public static void main(String args[])
{
// Get the file
File f = new File("F:\\program.txt");
// Get the last modified time of the file
System.out.println("Last modified: "
+ f.lastModified());
}
}
输出:
Last modified: 1542654265978
范例2:文件“F:\\program1.txt”不存在
// Java program to demonstrate
// lastModified() method of File Class
import java.io.*;
public class solution {
public static void main(String args[])
{
// Get the file
File f = new File("F:\\program.txt");
// Get the last modified time of the file
System.out.println("Last modified: "
+ f.lastModified());
}
}
输出:
Last modified: 0
注意:程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的路径。
本文介绍了Java中File类的lastModified()函数,该函数返回抽象路径名最后一次修改的时间,以毫秒为单位的long值,若文件不存在或异常则返回0L。文中给出函数签名、用法、返回类型及异常情况,并通过两个示例程序展示其具体使用。
634

被折叠的 条评论
为什么被折叠?



