java 读取文件修改时间
时间: 2023-10-15 18:25:40 浏览: 71
你可以使用Java的文件类(File)来读取文件的修改时间。以下是一个示例代码:
```java
import java.io.File;
import java.text.SimpleDateFormat;
public class FileModificationTime {
public static void main(String[] args) {
// 指定文件路径
String filePath = "path/to/your/file.txt";
// 创建File对象
File file = new File(filePath);
// 获取文件的最后修改时间
long lastModified = file.lastModified();
// 将时间戳转换为可读格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String modificationTime = dateFormat.format(lastModified);
// 打印修改时间
System.out.println("文件 " + file.getName() + " 的最后修改时间为:" + modificationTime);
}
}
```
请将`filePath`变量替换为你要读取的文件的路径。运行上述代码,将会打印出该文件的最后修改时间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)