当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。