大多数的文件系统都支持一些通用文件属性(文件大小、文件创建时间、最后访问时间、最后编辑时间、等等)。这些通用属性都被分组到 BasicFileAttributeView 中,下面将介绍如何读取和设置这些属性。
使用 readAttributes() 方法批量获取属性
可以通过调用 readAttributes() 方法来批量读取文件属性,这个方法如果使用不定长参数,那么默认就是使用了 LinkOption.NOFOLLOW_LINKS 这个枚举类型——这表示将不会处理符号链接所链接的文件,而只会处理符号链接本身的文件。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
…
BasicFileAttributes attr = null;
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
attr = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException e) {
System.err.println(e);
}
System.out.println("File size: " + attr.size());
System.out.println("File creation time: " + attr.creationTime());
System.out.println("File was last accessed at: " + attr.lastAccessTime());
System.out.println("File was last modified at: " + attr.lastModifiedTime());
System.out.println("Is directory? " + attr.isDirectory());
System.out.println("Is regular file? " + attr.isRegularFile());
System.out.println("Is symbolic link? " + attr.isSymbolicLink());
System.out.println("Is other? " + attr.isOther());
使用 getAttribute() 方法获取单个属性
如果你需要获取单个属性而不是获取全部的属性,那么可以使用 getAttribute() 方法。这个方法可传入文件的 path 对象、需要获取的属性名称和是否需要支持符号链接。下面的代码片段将演示如何获取文件大小。记住 getAttribute() 方法的返回值是 Object 类型,因此你需要对结果进行强制转换:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
long size = (Long)Files.getAttribute(path, "basic:size", NOFOLLOW_LINKS);
System.out.println("Size: " + size);
} catch (IOException e) {
System.err.println(e);
}
Basic 属性视图支持的属性名称如下:
- lastModifiedTime
- lastAccessTime
- creationTime
- size
- isRegularFile
- isDirectory
- isSymbolicLink
- isOther
- fileKey
如上面例子中所示,获取属性的格式为 [view-name:]attribute-name。在上面的例子中 view-name 是 basic,attribute-name 是 size。
编辑基本属性
更新文件的最后编辑时间、最后访问时间和创建时间属性可以调用 setTimes() 方法,这个方法的三个参数分别对应了最后编辑时间、最后访问时间以及创建时间。参数接受 FileTime 类型的对象,这个类是在 Java 7 中新引进的用来表示文件时间戳属性的类。如果不需要变更其中某个时间,则可以设置为 null。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
long time = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(time);
try {
Files.getFileAttributeView(path,
BasicFileAttributeView.class).setTimes(fileTime, fileTime, fileTime);
} catch (IOException e) {
System.err.println(e);
}
更新文件的最后编辑时间也可以调用 Files.setLastModifiedTime() 方法:
long time = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(time);
try {
Files.setLastModifiedTime(path, fileTime);
} catch (IOException e) {
System.err.println(e);
}
当然也可以通过通用的 setAttribute() 来更新文件的最后编辑时间,另外文件最后访问时间和文件创建时间也可以通过这个方法来设置:
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
…
try {
Files.setAttribute(path, "basic:lastModifiedTime", fileTime, NOFOLLOW_LINKS);
Files.setAttribute(path, "basic:creationTime", fileTime, NOFOLLOW_LINKS);
Files.setAttribute(path, "basic:lastAccessTime", fileTime, NOFOLLOW_LINKS);
} catch (IOException e) {
System.err.println(e);
}
现在,你可以检查文件的这三个属性是否已经改变。可以调用 getAttribute() 方法进行查看:
try {
FileTime lastModifiedTime = (FileTime)Files.getAttribute(path,
"basic:lastModifiedTime", NOFOLLOW_LINKS);
FileTime creationTime = (FileTime)Files.getAttribute(path,
"basic:creationTime", NOFOLLOW_LINKS);
FileTime lastAccessTime = (FileTime)Files.getAttribute(path,
"basic:lastAccessTime", NOFOLLOW_LINKS);
System.out.println("New last modified time: " + lastModifiedTime);
System.out.println("New creation time: " + creationTime);
System.out.println("New last access time: " + lastAccessTime);
} catch (IOException e) {
System.err.println(e);
}
文章来源:
http://www.aptusource.org/2014/03/nio-2-basic-view/
分享到:
相关推荐
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...
Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed...
java NIO.zip
这是一本介绍java io以及nio相关知识的书,书中对知识的讲解通俗易懂,是学习java nio以及复习java io相关知识的必备书籍。注意:本书为英文版!!!
java nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava ...
Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf
Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制
蔚来-NIO.US-新车上市,蔚来可期.pdf
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
Apress.Pro.Java.7.NIO.2.2011
Pro Java 7 NIO.2 – PDF Books
Pro Java 7 NIO.2.pdf,2011 by Anghel Leonard
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
找了好久,终于找到了,java刷新同步获取网络资源
JavaNIO.pdf
定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 NIO API 的集中抽象为: 缓冲区,它们是数据容器; 字符集 及其相关解码器 和编码器, 它们在字节和 Unicode 字符之间进行转换; 各种类型的通道,...
用java.nio.*进行网络编程
NIO入门.chm NIO入门.chm NIO入门.chm