📜  Java中的文件 setLastModified() 方法及示例

📅  最后修改于: 2022-05-13 01:54:38.246000             🧑  作者: Mango

Java中的文件 setLastModified() 方法及示例

setLastModified()方法是 File 类的一部分。该函数设置文件或目录的最后修改时间。该函数以毫秒为单位设置文件的最后修改值。
函数签名:

public boolean setLastModified(long time)

函数语法:

file.setLastModified(time)

参数:此函数接受一个长值作为参数,表示新的最后修改时间。
返回值:该函数返回一个布尔值,表示是否设置了新的最后修改时间。
例外;此方法引发以下异常:

  • 如果不允许该函数对文件进行写访问,则出现SecurityException
  • 如果参数为负,则IllegalArgumentException

下面的程序将说明 setLastModified()函数的使用:
示例 1:我们将尝试更改 f: 目录中现有文件的最后修改时间。

Java
// 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());
        }
    }
}


Java
// 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 is set

示例 2:我们将尝试更改 f: 目录中不存在的文件的最后修改时间。

Java

// 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并设置文件的父文件