在定义了 Path 对象后,你就可以调用它上面的一系列方法获取关于文件路径的有用信息。这些方法都基于一个前提条件,就是 NIO.2 将路径字符串分隔为一个个的元素(每个元素表示一个目录或文件的子路径),并且将元素由高到低分配索引 0 到 n-1(n 指元素的个数)。一般情况下,根下的第一个目录将会分配索引 0,随后索引递增。本文的例子都是基于 C:\rafaelnadal\tournaments\2009\BNP.txt 的 Path 对象:
Path path = Paths.get("C:", "rafaelnadal/tournaments/2009", "BNP.txt");
获取文件或目录名
调用 Path 对象的 getFileName() 方法将会返回离根目录最远的文件或文件夹名称:
//output: BNP.txt
System.out.println("The file/directory indicated by path: " + path.getFileName());
获取根目录 Path 对象
调用 Path 对象的 getRoot() 方法将会返回根目录 Path 对象,如果没有根目录则会返回 null:
//output: C:\
System.out.println("Root of this path: " + path.getRoot());
获取父目录 Path 对象
调用 Path 对象的 getParent() 方法将会返回父目录的 Path 对象,如果没有父目录则会返回 null:
//output: C:\rafaelnadal\tournaments\2009
System.out.println("Parent: " + path.getParent());
获取 Path 对象中的元素名称
调用 Path 对象的 getNameCount() 方法可以返回 Path 对象上所有元素的个数,再调用 getName() 方法可以获得每个元素的名称:
//output: 4
System.out.println("Number of name elements in path: " + path.getNameCount());
//output: rafaelnadal tournaments 2009 BNP.txt
for (int i = 0; i < path.getNameCount(); i++) {
System.out.println("Name element " + i + " is: " + path.getName(i));
}
获取 Path 中的子路径
调用 Path 对象的 subpath() 方法可以提取路径中的子路径信息,这个方法有两个参数分别表示元素索引的开始和结束:
//output: rafaelnadal\tournaments\2009
System.out.println("Subpath (0,3): " + path.subpath(0, 3));
文章来源:
http://www.aptusource.org/2014/03/nio-2-get-information-from-path/
分享到:
相关推荐
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 ...
java NIO.zip
New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 ...
这是一本介绍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.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...
Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf
Apress.Pro.Java.7.NIO.2.2011
蔚来-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 ...
Pro Java 7 NIO.2 – PDF Books
Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制
Pro Java 7 NIO.2.pdf,2011 by Anghel Leonard
JavaNIO.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 ...
找了好久,终于找到了,java刷新同步获取网络资源
定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 NIO API 的集中抽象为: 缓冲区,它们是数据容器; 字符集 及其相关解码器 和编码器, 它们在字节和 Unicode 字符之间进行转换; 各种类型的通道,...
用java.nio.*进行网络编程
在早期的 Java 版本中,文件 IO 操作功能一直相对较弱,主要存在以下...Path 路径:Paths 模块和 Path 工具类介绍 Files 文件:File 和 FileSystems 工具类介绍 文件管理服务:WatchService 、PathMatcher 等等文件服务