📜  用Java检查服务器上文件的最后修改

📅  最后修改于: 2022-05-13 01:54:23.970000             🧑  作者: Mango

用Java检查服务器上文件的最后修改

在Java中,我们有不同的类,如 File、URL,它们提供读取文件属性的功能,如创建时间、上次访问时间和上次修改时间。

方法一(使用BasicFileAttributes)

本示例使用Java.nio.*来显示文件的元数据和其他文件属性,如创建时间、上次访问时间和上次修改时间。

Java
// 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 GFG {
    public static void main(String args[])
    {
        // storing the path of the file in the string
        String fileName = "C:/Users/elavi/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();
        }
    }
}


Java
// Java Program to get last modification time of the file
import java.io.File;
import java.text.SimpleDateFormat;
  
public class GFG_Article {
  
    public static void main(String[] args)
    {
        // path of the file
        String fileName = "C:/Users/elavi/Desktop/File.txt";
  
        File file = new File(fileName);
  
        // getting the last modified time of the file in the
        // raw format ie 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()));
    }
}


Java
// 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 GFG {
    public static void main(String[] args) throws Exception
    {
          // resource url
        URL u = new URL(
            "https://www.geeksforgeeks.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);
    }
}


输出:输出包含文件中最后一次修改的日期和时间。

文件的最后修改时间

方法二(使用 File.lastModified)

对于legacy IO,可以使用File.lastModified() 方法来获取文件的最后修改时间,该函数返回文件的最后修改时间,以毫秒为单位的long值。我们可以使用 SimpleDataFormat 类使返回的结果更具可读性。

Java

// Java Program to get last modification time of the file
import java.io.File;
import java.text.SimpleDateFormat;
  
public class GFG_Article {
  
    public static void main(String[] args)
    {
        // path of the file
        String fileName = "C:/Users/elavi/Desktop/File.txt";
  
        File file = new File(fileName);
  
        // getting the last modified time of the file in the
        // raw format ie 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()));
    }
}

输出:输出包含文件中最后一次修改的日期和时间。

文件的最后修改时间

方法三(使用 URL 类)

检查最后 修改 上传文件在服务器上的时间,我们可以利用 URL 类,然后可以获取文件的最后修改时间。

Java

// 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 GFG {
    public static void main(String[] args) throws Exception
    {
          // resource url
        URL u = new URL(
            "https://www.geeksforgeeks.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);
    }
}

输出

文件的最后修改时间