| 1) NIO的通道类似于流,但有些区别如下: |
| • 通道可以同时进行读写,而流只能读或者只能写 |
| • 通道可以实现异步读写数据 |
| • 通道可以从缓冲读数据,也可以写数据到缓冲 |

| 2) BIO 中的 stream 是单向的,例如 FileInputStream 对象只能进行读取数据的操作,而 NIO 中的通道(Channel)是双向的,可以读操作,也可以写操作。 |
| 3) Channel在NIO中是一个接口public interface Channel extends Closeable{} |
| 4) 常用的 Channel 类有:FileChannel、DatagramChannel、ServerSocketChannel 和SocketChannel。【ServerSocketChanne 类似ServerSocket , SocketChannel 类似 Socket】 |
| 5) FileChannel 用于文件的数据读写,DatagramChannel 用于 UDP 的数据读写,ServerSocketChannel 和 SocketChannel 用于 TCP的数据读写。 |
| FileChannel主要用来对本地文件进行 IO 操作,常见的方法有 |
| 1) public int read(ByteBuffer dst) ,从通道读取数据并放到缓冲区中 |
| 2) public int write(ByteBuffer src) ,把缓冲区的数据写到通道中 |
| 3) public long transferFrom(ReadableByteChannel src, long position, long count),从目标通道中复制数据到当前通道 |
| 4) public long transferTo(long position, long count, WritableByteChannel target),把数据从当前通道复制给目标通道 |
| 1) 使用前面学习后的ByteBuffer(缓冲) 和 FileChannel(通道), 将 "hello,尚硅谷" 写入到file01.txt 中 |
| 2) 文件不存在就创建 |
| |
| public class NIOFileChannel01 { |
| public static void main(String[] args) throws Exception{ |
| String str = "hello,尚硅谷"; |
| |
| FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt"); |
| |
| |
| |
| FileChannel fileChannel = fileOutputStream.getChannel(); |
| |
| |
| ByteBuffer byteBuffer = ByteBuffer.allocate(1024); |
| |
| |
| byteBuffer.put(str.getBytes()); |
| |
| |
| |
| byteBuffer.flip(); |
| |
| |
| fileChannel.write(byteBuffer); |
| fileOutputStream.close(); |
| } |
| } |

- 案例2

| 1) 使用前面学习后的ByteBuffer(缓冲) 和 FileChannel(通道), 将 file01.txt 中的数据读入到程序,并显示在控制台屏幕 |
| 2) 假定文件已经存在 |
| |
| public class NIOFileChannel02 { |
| public static void main(String[] args) throws Exception { |
| |
| |
| File file = new File("d:\\file01.txt"); |
| FileInputStream fileInputStream = new FileInputStream(file); |
| |
| |
| FileChannel fileChannel = fileInputStream.getChannel(); |
| |
| |
| ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); |
| |
| |
| fileChannel.read(byteBuffer); |
| |
| |
| System.out.println(new String(byteBuffer.array())); |
| fileInputStream.close(); |
| |
| } |
| } |
- 案例3

| 1) 使用 FileChannel(通道) 和 方法 read , write,完成文件的拷贝 |
| 2) 拷贝一个文本文件 1.txt , 放在项目下即可 |
| |
| public class NIOFileChannel03 { |
| public static void main(String[] args) throws Exception { |
| |
| FileInputStream fileInputStream = new FileInputStream("1.txt"); |
| FileChannel fileChannel01 = fileInputStream.getChannel(); |
| |
| FileOutputStream fileOutputStream = new FileOutputStream("2.txt"); |
| FileChannel fileChannel02 = fileOutputStream.getChannel(); |
| |
| ByteBuffer byteBuffer = ByteBuffer.allocate(512); |
| |
| while (true) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| byteBuffer.clear(); |
| int read = fileChannel01.read(byteBuffer); |
| System.out.println("read =" + read); |
| if(read == -1) { |
| break; |
| } |
| |
| byteBuffer.flip(); |
| fileChannel02.write(byteBuffer); |
| } |
| |
| |
| fileInputStream.close(); |
| fileOutputStream.close(); |
| } |
| } |
| 1) 使用 FileChannel(通道) 和 方法 transferFrom ,完成文件的拷贝 |
| 2) 拷贝一张图片 |
| |
| public class NIOFileChannel04 { |
| public static void main(String[] args) throws Exception { |
| |
| |
| FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg"); |
| FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg"); |
| |
| |
| FileChannel sourceCh = fileInputStream.getChannel(); |
| FileChannel destCh = fileOutputStream.getChannel(); |
| |
| |
| destCh.transferFrom(sourceCh,0,sourceCh.size()); |
| |
| sourceCh.close(); |
| destCh.close(); |
| fileInputStream.close(); |
| fileOutputStream.close(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?