當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java File setLastModified()用法及代碼示例


setLastModified()方法是File類的一部分。該函數設置文件或目錄的最後修改時間。該函數以毫秒為單位設置文件的最後修改值。

函數簽名:

public boolean setLastModified(long time)

函數語法:


file.setLastModified(time)

參數:此函數接受一個long值作為參數,該值代表新的上次修改時間。

返回值:該函數返回一個布爾值,該值指示是否設置了新的上次修改時間。

例外;此方法引發以下異常:

  • SecurityException如果不允許該函數對文件的寫訪問
  • IllegalArgumentException如果論點是消極的

下麵的程序將說明setLastModified()函數的用法:

範例1:我們將嘗試更改f:目錄中現有文件的最後修改時間。

// Java program to demonstrate the 
// use of setLastModified() function 
  
import java.io.*; 
  
public class solution { 
    public static void main(String args[]) 
    { 
  
        // try-catch block to handle exceptions 
        try { 
  
            // Create a file object 
            File f = new File("f:\\program.txt"); 
  
            // The new last modified time 
            long time = 100000000; 
  
            // Check if the last modified time 
            // can be set to new value 
            if (f.setLastModified(time)) { 
  
                // Display that the last modified time 
                // is set as the function returned true 
                System.out.println("Last modified time is set"); 
            } 
            else { 
  
                // Display that the last modified time 
                // cannot be set as the function returned false 
                System.out.println("Last modified time cannot be set"); 
            } 
        } 
        catch (Exception e) { 
            System.err.println(e.getMessage()); 
        } 
    } 
}

輸出:

Last modified time is set

範例2:我們將嘗試更改f:目錄中不存在的文件的最後修改時間。

// Java program to demonstrate the 
// use of setLastModified() function 
  
import java.io.*; 
  
public class solution { 
    public static void main(String args[]) 
    { 
  
        // try-catch block to handle exceptions 
        try { 
  
            // Create a file object 
            File f = new File("f:\\program1.txt"); 
  
            // The new last modified time 
            long time = 100000000; 
  
            // Check if the last modified time 
            // can be set to new value 
            if (f.setLastModified(time)) { 
  
                // Display that the last modified time 
                // is set as the function returned true 
                System.out.println("Last modified "
                                   + "time is set"); 
            } 
            else { 
  
                // Display that the last modified time 
                // cannot be set as 
                // the function returned false 
                System.out.println("Last modified time"
                                   + " cannot be set"); 
            } 
        } 
        catch (Exception e) { 
            System.err.println(e.getMessage()); 
        } 
    } 
}

輸出:

Last modified time cannot be set

這些程序可能無法在在線IDE中運行。請使用離線IDE並設置文件的父文件



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 File setLastModified() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。