NIO概述
- Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,**NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。**NIO将以更加高效的方式进行文件的读写操作。
- Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。
java.nio.channels.Channel
- FileChannel: 处理本地文件 (标准输入输出NIO)
- SocketChannel: TCP网络编程的客户端的Channel
- ServerSocketChannel: TCP网络编程的服务器端的Channel
- DatagramChannel:UDP网络编程中发送端和接收端的Channel
- 随着 JDK 7 的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特性的支持,以至于我们称他们为 NIO.2。
核心API
- 早期java只提供了File类访问文件系统,File类功能有限,提供的性能也不高,并且大多数方法在出错时仅返回失败,并不会提供异常信息.
- NIO.2为了弥补不足,引入了Path接口,代表一个平台无关的平台路径,扫描了目录结构中文件的置.Path可以看成是File类的升级版本,实际引用的资源也可以不存在。
- NIO.2在java.nio.file包下还提供了Files、Paths工具类,Files包含了大量静态的工具方法来操作文件;Paths则包含了两个返回Path的静态工厂方法。
- Paths 类提供的静态 get() 方法用来获取 Path 对象:
static Path get(String first, String … more)
: 用于将多个字符串串连成路径
static Path get(URI uri)
: 返回指定uri对应的Path路径
- 示例:
import java.io.File;
File file = new File("index.html");
import java.nio.file.Path;
import java.nio.file.Paths;
Path path = Paths.get("index.html");
Path接口
String toString() : 返回调用 Path 对象的字符串表示形式
boolean startsWith(String path) : 判断是否以 path 路径开始
boolean endsWith(String path) : 判断是否以 path 路径结束
boolean isAbsolute() : 判断是否是绝对路径
Path getParent() :返回Path对象包含整个路径,不包含 Path 对象指定的文件路径
Path getRoot() :返回调用 Path 对象的根路径
Path getFileName() : 返回与调用 Path 对象关联的文件名
int getNameCount() : 返回Path 根目录后面元素的数量
Path getName(int idx) : 返回指定索引位置 idx 的路径名称
Path toAbsolutePath() : 作为绝对路径返回调用 Path 对象 (***)
Path resolve(Path p) :合并两个路径,返回合并后的路径对应的Path对象
File toFile(): 将Path转化为File类的对象 (***)
Files类
Path copy(Path src, Path dest, CopyOption … how) : 文件的复制
Path createDirectory(Path path, FileAttribute<?> … attr) : 创建一个目录 (***)
Path createFile(Path path, FileAttribute<?> … arr) : 创建一个文件 (***)
void delete(Path path) : 删除一个文件/目录,如果不存在,执行报错
void deleteIfExists(Path path) : Path对应的文件/目录如果存在,执行删除 (***)
Path move(Path src, Path dest, CopyOption…how) : 将 src 移动到 dest 位置
long size(Path path) : 返回 path 指定文件的大小 (***)
boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
boolean isRegularFile(Path path, LinkOption … opts) : 判断是否是文件
boolean isHidden(Path path) : 判断是否是隐藏文件
boolean isReadable(Path path) : 判断文件是否可读
boolean isWritable(Path path) : 判断文件是否可写
boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在
SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 获取与指定文件的连接,how 指定打开方式。
DirectoryStream<Path> newDirectoryStream(Path path) : 打开 path 指定的目录
InputStream newInputStream(Path path, OpenOption…how):获取 InputStream 对象
OutputStream newOutputStream(Path path, OpenOption…how) : 获取 OutputStream 对象
示例
Path接口示例
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathTest {
@Test
public void test1() {
Path path1 = Paths.get("d:\\nio\\hello.txt");
Path path2 = Paths.get("d:\\", "nio\\hello.txt");
System.out.println(path1);
System.out.println(path2);
Path path3 = Paths.get("d:\\", "nio");
System.out.println(path3);
}
@Test
public void test2() {
Path path1 = Paths.get("d:\\", "nio\\nio1\\nio2\\hello.txt");
Path path2 = Paths.get("hello.txt");
System.out.println(path1);
System.out.println(path1.startsWith("d:\\nio"));
System.out.println(path1.endsWith("hello.txt"));
System.out.println(path1.isAbsolute() + "~");
System.out.println(path2.isAbsolute() + "~");
System.out.println(path1.getParent());
System.out.println(path2.getParent());
System.out.println(path1.getRoot());
System.out.println(path2.getRoot());
System.out.println(path1.getFileName() + "~");
System.out.println(path2.getFileName() + "~");
for (int i = 0; i < path1.getNameCount(); i++) {
System.out.println(path1.getName(i) + "*****");
}
System.out.println(path1.toAbsolutePath());
System.out.println(path2.toAbsolutePath());
Path path3 = Paths.get("d:\\", "nio");
Path path4 = Paths.get("nioo\\hi.txt");
path3 = path3.resolve(path4);
System.out.println(path3);
File file = path1.toFile();
Path newPath = file.toPath();
}
}
Files类示例
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.util.Iterator;
public class FilesTest {
@Test
public void test1() throws IOException{
Path path1 = Paths.get("d:\\nio", "hello.txt");
Path path2 = Paths.get("atguigu.txt");
Path path3 = Paths.get("d:\\nio\\nio1");
Path path4 = Paths.get("d:\\nio\\hi.txt");
Files.deleteIfExists(path3);
long size = Files.size(path2);
System.out.println(size);
}
@Test
public void test2() throws IOException{
Path path1 = Paths.get("d:\\nio", "hello.txt");
Path path2 = Paths.get("atguigu.txt");
System.out.println(Files.exists(path2, LinkOption.NOFOLLOW_LINKS));
System.out.println(Files.isDirectory(path1, LinkOption.NOFOLLOW_LINKS));
System.out.println(Files.isReadable(path1));
System.out.println(Files.isWritable(path1));
System.out.println(Files.notExists(path1, LinkOption.NOFOLLOW_LINKS));
}
@Test
public void test3() throws IOException{
Path path1 = Paths.get("d:\\nio", "hello.txt");
InputStream inputStream = Files.newInputStream(path1, StandardOpenOption.READ);
OutputStream outputStream = Files.newOutputStream(path1, StandardOpenOption.WRITE,StandardOpenOption.CREATE);
SeekableByteChannel channel = Files.newByteChannel(path1, StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
Path path2 = Paths.get("e:\\teach");
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path2);
Iterator<Path> iterator = directoryStream.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
使用jar包
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileUtilsTest {
public static void main(String[] args) {
File srcFile = new File("day10\\爱情与友情.jpg");
File destFile = new File("day10\\爱情与友情2.jpg");
try {
FileUtils.copyFile(srcFile,destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}