在 Java 中检查服务器上文件的最后修改时间
文件的最后修改时间
在当今的数字时代,许多计算机程序都依赖于访问和修改服务器上文件的关键方面。然而,为了确保使用最新信息,确定文件的最后修改时间通常是必要的。在 Java 中,有多种方法可以确认服务器上文件的最后修改时间,每种方法都有其优点和缺点。
方法 1:(使用 file.lastModified)
可以使用 File 类在 Java 中访问各种文件属性,包括检查服务器文件的最后修改时间。通过使用 lastModified() 方法,一旦创建了表示服务器文件的 File 对象,就可以轻松访问最后修改时间。使用 Date 类,此方法可以将自纪元以来的毫秒时间转换为更易读的格式。
示例
// Java Program to get last modification time of the file
import java.io.File;
import java.text.SimpleDateFormat;
public class Sample_Article {
public static void main(String[] args) {
// path of the file
String fileName = "C:/Users/aashi/Desktop/File.txt";
File file = new File(fileName);
// getting the last modified time of the file in the
// raw format I.e long value of milliseconds
System.out.println("Before Format : "+ file.lastModified());
// getting the last modified time of the file in
// terms of time and date
SimpleDateFormat sdf
= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(
"The Date and Time at which the file was last modified is "
+ sdf.format(file.lastModified()));
}
}
输出
Before Format : 0 The Date and Time at which the file was last modified is 01/01/1970 00:00:00
方法 2:(使用基本文件属性)
在 Java 中检查服务器上文件的最后修改时间的另一种方法是使用基本文件属性。通过使用 java.nio.*,可以方便地查看文件的元数据和各种属性,例如创建时间、最后访问时间和最后修改时间。
示例
// Java Program to get last modification time of the file
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
public class Sample_Article {
public static void main(String args[]) {
// storing the path of the file in the string
String fileName = "C:/Users/aashi/Desktop/File.txt";
// used exception handling in order to catch the
// exception
// which may occur if there is no such file is
// present at specified location
// or the path of the file provided by the user is
// incorrect
try {
// getting the path of the file
Path file = Paths.get(fileName);
// reading the attributes of the file
BasicFileAttributes attr = Files.readAttributes(
file, BasicFileAttributes.class);
// getting the last modification time of the
// file
System.out.println(
"lastModifiedTime of File Is : "+ attr.lastModifiedTime());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
输出
LastModifiedTime of a file is: 2023-05-02 T: 13:25:20
方法 3:(使用 URL 类)
在 Java 中检查服务器上文件的最后修改时间的第三种方法是使用 URL 类。这提供了一种检索上传文件的最后修改时间和检索文件时间戳的方法。此方法需要身份验证才能访问服务器。
示例
// Java Program to get last modification time of the file
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Date;
public class Sample_Article {
public static void main(String[] args) throws Exception{
// resource url
URL u = new URL(
"https://www.waytoclass.org/file-handling-java-using-filewriter-filereader/");
URLConnection uc = u.openConnection();
uc.setUseCaches(false);
// getting the last modified time of the file
// uploaded on the server
long timestamp = uc.getLastModified();
System.out.println("The last modification time of java.bmp is:"
+ timestamp);
}
}
输出
The last modification time of java.bmp is:0
结论
在 Java 中,有多种方法可以确定服务器上的文件最后修改时间,每种方法都有其优点和缺点。BasicFile Attributes 函数允许远程访问文件信息检索,而 File 类提供了一种基本且易于使用的方法来检索文件属性。虽然需要身份验证,但 URL 类方法提供了一种更安全的方式来查看和编辑远程服务器上的文件。使用哪种策略将取决于程序的具体需求和可用资源。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP