在 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 類方法提供了一種更安全的方式來檢視和編輯遠端伺服器上的檔案。使用哪種策略將取決於程式的具體需求和可用資源。

更新於: 2023年8月1日

251 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.