Java文件类boolean setLastModified(long set_new_time)方法,包含示例

文件类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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值