Java程式獲取檔案的最後修改日期
檔案的最後修改日期指的是檔案資料最後一次被編輯的日期。Java中有多種函式可以查詢檔案的最後修改日期。在本節中,我們將討論實現Java程式以獲取檔案最後修改日期的不同方法。
檔案是資訊的集合,可能包含文字資訊、影像、音訊、影片、程式程式碼等資料。任何軟體應用程式都可以訪問這些檔案以執行讀取、寫入、更新、刪除等操作。
我們現在將研究我們在下面的示例中將使用的每個函式。
建立檔案類物件:
File file = new File("file_path");
exists() − 此方法檢查檔案是否存在,並返回布林值。
File file = new File("/example.txt");
if (file.exists()) {
System.out.println("File exists.");
}
建立路徑類物件
Path path = Paths.get("path_string");
建立一個SimpleDateFormat類物件
SimpleDateFormat formatter = new SimpleDateFormat("pattern");
lastModified() − 此方法用於返回檔案最後修改的時間。它位於java.io包中。它以檔案物件作為輸入。
File file = new File("path/to/my/file.txt");
long lastModified = file.lastModified();
toMillis() − 此方法將FileTime物件轉換為毫秒。它位於java.nio.file.attribute包中。
Path file = Path.of("path/to/my/file.txt");
FileTime lastModifiedTime = Files.getLastModifiedTime(file);
long millis = lastModifiedTime.toMillis();
getLastModifiedTime() − 此方法用於透過獲取路徑物件作為引數來返回檔案的最後修改時間。
Path file = Path.of("path/to/my/file.txt");
FileTime lastModifiedTime = Files.getLastModifiedTime(file);
format() − 此方法使用指定的格式將日期轉換為字串。它接受日期作為引數並返回字串。此函式在SimpleDateFormat物件上呼叫。
Date now = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(now);
現在,我們將介紹在Java中查詢檔案最後修改日期的不同程式碼實現。
方法1:使用java.io包
在這種方法中,我們將實現Java程式碼,使用內建的File類和java.io包來獲取檔案的最後修改日期。
步驟
使用File類建立一個檔案物件。
使用exists()方法檢查檔案是否存在,如果檔案存在,則使用lastModified()方法獲取最後修改時間。
使用Date類和最後修改時間建立一個日期物件。
建立一個SimpleDateFormat物件並格式化日期。
列印日期。
如果檔案不存在,則列印“檔案未找到”。
示例
在這個例子中,我們使用檔案類建構函式建立了一個file物件,並使用‘exists()’方法檢查檔案是否存在。如果檔案存在,我們就在file物件上使用了lastModified()方法,該方法以毫秒為單位返回最後修改的時間。然後,我們使用Date建構函式並將儲存在‘lastModified’變數中的lastModified值傳遞給Date建構函式,以獲取日期物件。然後,我們使用SimpleDateFormat建構函式透過傳遞任何特定的日期格式作為引數(在本例中為"dd/MM/yyyy HH:mm:ss")建立一個SimpleDateFormat物件,並建立格式物件。現在,使用SimpleDateFormat物件,我們將使用format()函式並將日期物件傳遞給它,以獲取格式化的日期作為輸出。
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
File file = new File("C:/example.txt");
if (file.exists()) {
long lastModified = file.lastModified();
Date date = new Date(lastModified);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = formatter.format(date);
System.out.println("Last modified date of file: " + formattedDate);
} else {
System.out.println("File not found.");
}
}
}
輸出
Last modified date of file: 28/02/2023 16:32:45
方法2:使用java.nio包
在這種方法中,我們將實現Java程式碼,使用內建的Path類和java.nio包來獲取檔案的最後修改日期。
步驟
匯入所有必需的Java庫。
使用Path類建立一個路徑物件。
使用Files類使用getLastModifiedTime()函式並傳遞建立的路徑物件來查詢最後修改時間。
使用Date類和最後修改時間建立一個日期物件。
建立一個SimpleDateFormat物件並格式化日期。
列印日期。
如果檔案不存在,則列印“檔案未找到”。
示例
在這個例子中,我們使用路徑類建構函式建立了一個path物件,然後我們在path物件上使用了lastModified()方法,該方法以毫秒為單位返回最後修改的時間。然後,我們使用Date建構函式並將儲存在‘lastModified’變數中的lastModified值傳遞給Date建構函式,以獲取日期物件。然後,我們使用SimpleDateFormat建構函式透過傳遞任何特定的日期格式作為引數(在本例中為"dd/MM/yyyy HH:mm:ss")建立一個SimpleDateFormat物件,並建立格式物件。現在,使用SimpleDateFormat物件,我們將使用format()函式並將日期物件傳遞給它,以獲取格式化的日期作為輸出。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Path file = Path.of("path/to/my/file.txt");
try {
FileTime lastModifiedTime = Files.getLastModifiedTime(file);
Date date = new Date(lastModifiedTime.toMillis());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = formatter.format(date);
System.out.println("Last modified date: " + formattedDate);
} catch (IOException e) {
System.out.println("File not found.");
}
}
}
輸出
Last modified date of file: 28/02/2023 16:32:45
因此,在這篇文章中,我們討論了使用Java程式語言獲取檔案最後修改日期的不同方法。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP