文件類boolean setLastModified(long set_new_time) (File Class boolean setLastModified(long set_new_time))
This method is available in package java.io.File.setLastModified(long set_new_time).
軟件包java.io.File.setLastModified(long set_new_time)中提供了此方法。
This method is used to sets the last modified time of the given file.
此方法用于設置給定文件的最后修改時間。
In other words, Every file has some last modified time if file exists but by using this method we can change the last modified time of the file with the given new time.
換句話說,如果文件存在,則每個文件都有最后修改時間,但是通過使用此方法,我們可以使用給定的新時間來更改文件的最后修改時間。
The return type of this method is Boolean so it returns true if file last modification is set successfully else return false.
此方法的返回類型為Boolean,因此如果成功設置了文件上次修改,則返回true,否則返回false。
Syntax:
句法:
boolean setLastModified(long set_new_time){
}
Parameter(s):
參數:
We pass only one object as a parameter in the method of the file i.e. set_new_time (We will set new time as last modified time of the file).
在文件方法中,我們僅傳遞一個對象作為參數,即set_new_time (我們將新時間設置為文件的最后修改時間)。
Return value:
返回值:
The return type of this method is Boolean, it returns true if the given time is set successfully of last modification of the file else it returns false.
此方法的返回類型為Boolean ,如果成功設置了文件的上一次修改的給定時間,則返回true ,否則返回false 。
Java程序演示setLastModified()方法的示例 (Java program to demonstrate example of setLastModified() method)
import java.io.*;
public class SetFileLastModifiedTime {
public static void main(String[] args) {
try {
// Create a file object file1
File file1 = new File("E:\\Programs\\myjava.txt");
// Previous Last Modified Time Of The File1
System.out.println("The Previous Last Modification Time Of File1 is : " + file1.lastModified());
// By using setLastModified(long set_new_time) method sets
// new last modified time of the file.
System.out.println("The New Last Modification Time has set : " + file1.setLastModified(2000000));
// Display New Last Modified Time Of The File1
System.out.println("The New Last Modification Time Of File1 is : " + file1.lastModified());
} catch (Exception e) {
System.err.println("An error occurred");
e.printStackTrace();
}
}
}
Output
輸出量
E:\Programs>javac SetFileLastModifiedTime.java
E:\Programs>java SetFileLastModifiedTime
The Previous Last Modification Time Of File1 is : 1000000
The New Last Modification Time Of File1 has set : true
The New Last Modification Time Of File1 is : 2000000
翻譯自: https://www.includehelp.com/java/file-class-boolean-setlastmodified-long-set_new_time-method-with-example.aspx