传统的Java里,只有一个File类,即代表文件,又代表目录。Java 7新增了如下API来访问文件
Path - 接口,代表一个平台无关的目录。提供了大量的方法来操作目录。
Paths - 工具类。所有方法都是static的。
Files - 操作文件的工具类。提供了大量的方法来操作文件。该类所包含的大量方法可能与我们日常一般的期望有些出入。
早期的Java只提供了File类来访问文件,其功能有限且性能不高,NIO.2提供了Path接口以及Paths和Files工具类来访问文件系统。
1.Path接口和Paths工具类
java.nio.file.Path
Path接口代表与平台无关的路径.
An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.
java.nio.file.Paths
This class consists exclusively of static methods that return a Path by converting a path string or URI.
部分方法介绍:
1.getNameCount
int getNameCount()
返回当前Path对象里包含的路径数量。
Returns the number of name elements in the path.
Returns the number of elements in the path, or 0 if this path only represents a root component.
2.getRoot
Path getRoot()
获取当前Path对象的根路径
Returns the root component of this path as a Path object, or null if this path does not have a root component.
Returns:a path representing the root component of this path, or null.
3.toAbsolutePath
Path toAbsolutePath()
获取当前Path对象对应的绝对路径。
Returns a Path object representing the absolute path of this path.
If this path is already absolute then this method simply returns this path.
Otherwise, this method resolves the path in an implementation dependent manner,
typically by resolving the path against a file system default directory.
Depending on the implementation, this method may throw an I/O error if the file system is not accessible.
Returns:a Path object representing the absolute path.
2.Files工具类
操作文件的工具类。由于Files工具类强大的功能,可以使用Files类来简化文件IO。
java.nio.file.Files
public final class Files extends Object
This class consists exclusively of static methods that operate on files, directories, or other types of files.
In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.
下面就通过几个简单的操作演示NIO的强大功能吧!
package test.jse.io.nio2.demo1; import java.io.FileOutputStream; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class NIO2Test { public static void main(String[] args) throws Exception { //testPath(); testFiles(); } public static void testPath() { //以当前路径创建Path对象 Path path=Paths.get("."); System.out.println("当前Path对象里包含的路径数量: "+path.getNameCount()); System.out.println("当前Path对象的根路径: "+path.getRoot()); //获取path对应的绝对路径 Path absolutePath=path.toAbsolutePath(); System.out.println("path对应的绝对路径: "+absolutePath); System.out.println("当前absolutePath对象里包含的路径数量: "+absolutePath.getNameCount()); } public static void testFiles() throws Exception { //复制文件 Files.copy(Paths.get("src/test/jse/io/nio2/demo1/NIO2Test.java"),new FileOutputStream("src/test/jse/io/nio2/demo1/new_NIO2Test.java")); //判断NIO2Test.java文件是否为隐藏文件 System.out.println("NIO2Test.java文件是否为隐藏文件:\t"+Files.isHidden(Paths.get("src/test/jse/io/nio2/demo1/NIO2Test.java"))); //一次读取NIO2Test.java文件的所有行 List<String> lines=Files.readAllLines(Paths.get("src/test/jse/io/nio2/demo1/NIO2Test.java"), Charset.forName("gbk")); for (String line : lines) { System.out.println(line); } } }
API文档中还有大量的方法可以简化流与文件的操作,希望读者朋友们多多尝试与总结,并在实际开发中运用!