本例以Windows, JDK1.7 为基础,来演示Java 对文件或文件夹信息的更新操作。
一,示例代码
演示对文件的最新更新日期,可读性进行操作
FileInfoUpdate.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.devnp.io;
import java.io.File; import java.text.SimpleDateFormat;
public class FileInfoUpdate {
public static void main(String[] args) { fileInfoUpdate(); } public static void fileInfoUpdate(){ SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); File file = new File("D:\\Person-Test\\5.pdf"); System.out.println("Last Modified : " + sdf.format(file.lastModified())); file.setLastModified(System.currentTimeMillis()) ; System.out.println("Last Modified : " + sdf.format(file.lastModified())); System.out.println("File Can Wirte : " + file.canWrite()); file.setReadOnly() ; System.out.println("File Can Wirte : " + file.canWrite()); }
}
|
测试结果:
[code lang="test"]
Last Modified : 03/27/2017 23:34:49
Last Modified : 04/09/2017 23:08:07
File Can Wirte : true
File Can Wirte : false
```
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE