Java 检查服务器上文件的最后修改
文件的最后修改
现今数字时代,许多计算机程序依赖于访问和修改服务器上的文件这一关键方面。然而,为了确保使用最新信息,经常需要确定文件的最后修改时间。Java中存在几种策略来确认服务器上文件的最后修改,每种策略都有其优点和缺点。
方法1:(使用file.lastModified)
在Java中,可以通过File类来访问各种文件属性,包括检查服务器文件的最后修改。通过使用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:(使用BasicFile属性)
在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中确定服务器上的文件最后修改日期,每种方法都有其优缺点。 BasicFileAttributes函数允许远程访问以检索文件信息,而文件类则提供了一种基本且易于使用的方法来检索文件属性。虽然需要进行身份验证,但URL类方法提供了一种更安全的查看和编辑远程服务器上文件的方式。选择的策略将依赖于程序的特定要求和可用资源。