简介:Channel,称之为通道,在NIO中用于完成数据的传输;在操作的时候是面向缓冲区进行的;可以实现数据的双向传输;Channel默认是阻塞的,可以手动设置为非阻塞。
FileChannel
一、概述
1. FileChannel,顾名思义是面向文件的通道
2. 可以利用FileChannel完成对文件的读写操作
3. 利用FileChannel读取文件的时候,是先将文件中的内容映射到虚拟内存中,然后再读取到程序的缓冲区中
4. FileChannel不能直接创建,可以利用FileInputStream、FileOutputStream、RandomAccessFile对象中的个体Channel()方法获取
5. 如果是通过FileInputStream获取FileChannel,那么只能进行读取操作
6. 如果是通过FileOutputStream获取FileChannel,那么只能进行写入操作
7. 如果是通过RandomAccessFile获取FileChannel,那么可以进行读写操作
二、示例
读取过程
@Test
public void readFile() throws Exception {
// 创建RandomAccessFile对象。指定模式为读写模式
RandomAccessFile raf = new RandomAccessFile("F:\\a.txt", "rw");
// 获取FileChannel对象
FileChannel fc = raf.getChannel();
// 创建缓冲区用于存储数据
ByteBuffer buffer = ByteBuffer.allocate(10);
// 记录读取的字节个数
int len;
// 读取数据
while ((len = fc.read(buffer)) != -1) {
System.out.println(new String(buffer.array(), 0, len));
buffer.flip();
}
// 关流
raf.close();
}
写入过程
@Test
public void writeFile() throws Exception {
// 创建RandomAccessFile对象。指定模式为读写模式
RandomAccessFile raf = new RandomAccessFile("F:\\test.txt", "rw");
// 获取FileChannel对象
FileChannel fc = raf.getChannel();
// 创建缓冲区,并且将数据放入缓冲区
ByteBuffer src = ByteBuffer.wrap("hello".getBytes());
// 利用通道写出数据
fc.write(src);
// 关流
raf.close();
}
复制文件
@Test
public void copyFile() throws Exception {
// 创建流对象指向对应的文件
FileInputStream in = new FileInputStream("F:\\a.txt");
FileOutputStream out = new FileOutputStream("E:\\a.txt");
// 获取FileChannel对象
FileChannel src = in.getChannel();
FileChannel dest = out.getChannel();
// 准备缓冲区
ByteBuffer buffer = ByteBuffer.allocate(10);
// 读取数据,将读取到的数据写出
while (src.read(buffer) != -1) {
buffer.flip();
dest.write(buffer);
buffer.clear();
}
// 关流
in.close();
out.close();
}
UDP
一、概述
1. 用于进行UDP收发的通道
2. 是无连接的网络协议,只能进行发送和接受的操作
3. 基本类是DatagramChannel,是一个抽象类
二、示例
发送端
@Test
public void send() throws IOException {
// 开启通道
DatagramChannel dc = DatagramChannel.open();
// 准备数据
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
// 发送数据
dc.send(buffer, new InetSocketAddress("localhost", 8090));
// 关闭通道
dc.close();
}
接收端
@Test
public void recieve() throws IOException {
// 开启通道
DatagramChannel dc = DatagramChannel.open();
// 绑定连接地址和端口号
dc.socket().bind(new InetSocketAddress(8090));
// 准备缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 接收数据
dc.receive(buffer);
System.out.println(new String(buffer.array(), 0, buffer.position()));
// 关闭通道
dc.close();
}
TCP
一、概述
1. 用于进行TCP通信的通道
2. 需要进行连接的网络协议
3. 提供了连接、接收、读取、写入操作
4. 客户端通道是SocketChannel,服务器端通道是ServerSocketChannel
二、示例
客户端
package cn.tedu.nio.channel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Client {
public static void main(String[] args) throws IOException, InterruptedException {
// 创建客户端的通道
SocketChannel sc = SocketChannel.open();
// 设置为非阻塞
sc.configureBlocking(false);
// 连接过程在BIO中会产生阻塞
// 发起连接
sc.connect(new InetSocketAddress("localhost", 8070));
// 判断连接是否建立
while (!sc.isConnected())
// 如果没有连上,试图再次连接
// 如果连接多次依然失败,则意味着这个连接无效
// finishConnect底层会自动计数,计数多次依然失败则抛出异常
sc.finishConnect();
// 发送数据
sc.write(ByteBuffer.wrap("hello server".getBytes()));
Thread.sleep(10);
// 接收数据
ByteBuffer dst = ByteBuffer.allocate(1024);
sc.read(dst);
System.out.println(new String(dst.array(), 0, dst.position()));
// 关流
sc.close();
}
}
服务器端
package cn.tedu.nio.channel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
// 开启服务器端的通道
ServerSocketChannel ssc = ServerSocketChannel.open();
// 绑定监听端口
ssc.bind(new InetSocketAddress(8070));
// 设置位非阻塞
ssc.configureBlocking(false);
// 接收连接
SocketChannel sc = ssc.accept();
// 判断连接是否接收
// 服务器端是否要计数? - 不用
while (sc == null)
sc = ssc.accept();
// 接收数据
ByteBuffer buffer = ByteBuffer.allocate(1024);
sc.read(buffer);
buffer.flip();
System.out.println(new String(buffer.array(), 0, buffer.limit()));
// 打回数据
sc.write(ByteBuffer.wrap("数据已经收到~~~".getBytes()));
Thread.sleep(10);
// 关流
ssc.close();
}
}