栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

NIO之Channel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NIO之Channel

FileChannel
  • 一、通道(channel)介绍
  • 二、主要实现类
  • 三、获取通道
  • 四、案例
    • 4.1 FileChannel-文件复制
    • 4.2 SocketChannel、ServerSocketChannel
    • 4.3 DatagramChannel

一、通道(channel)介绍

Channel是一个对象,作用是用于源节点和目标节点的连接,在java NIO中负责缓冲区数据的传递。Channel本身不存储数据,因此需要配合缓冲区进行传输。
  

二、主要实现类

主要的实现类有如下四个: FileChannel, SocketChannel, ServerSocketChannel, DatagramChannel,

三、获取通道
  1. Java针对支持通道的类提供了getChannel()方法
本地IO网络IO
FileInputStream/FileOutputStreamSocket
RandomAccessFileServerSocket
无DatagramSocket
  1. 在JDK1.7中的NIO.2针对各个通道提供了静态方法open()
  2. 在JDK1.7中的NIO.2的Files工具类的newByteChannel()
public static void main(String[] args) throws Exception {
	// 1本地IO获取通道
	FileInputStream inputStream = new FileInputStream(resourceFilePath);
    FileOutputStream outputStream = new FileOutputStream(targetFilePath);
    FileChannel inChannel = inputStream.getChannel();
    FileChannel outChannel = outputStream.getChannel();
	
	// 2.通过open方法获取
	FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
	FileChannel.open(Paths.get(targetFilePath),
                        StandardOpenOption.READ,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.CREATE);
}
四、案例 4.1 FileChannel-文件复制
public class FileNioTest {
    public static void main(String[] args) {
        String resourceFilePath = "D:\test\copyFileTest.txt";
        String targetFilePath = "D:\test\copyFileTest-1.txt";
        // 复制文件
        // copyFile1(resourceFilePath, targetFilePath);
        // copyFile2(resourceFilePath, targetFilePath);
        copyFile3(resourceFilePath, targetFilePath);
    }

    private static void copyFile1(String resourceFilePath, String targetFilePath) {
        // 使用FileChannel配合缓冲区实现文件复制的功能
        try (
                FileInputStream inputStream = new FileInputStream(resourceFilePath);
                FileOutputStream outputStream = new FileOutputStream(targetFilePath);
                FileChannel inChannel = inputStream.getChannel();
                FileChannel outChannel = outputStream.getChannel();
        ) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            while (inChannel.read(buffer) != -1) {
                // 切换读模式
                buffer.flip();
                outChannel.write(buffer);
                // 清空
                buffer.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void copyFile2(String resourceFilePath, String targetFilePath) {
        // 内存映射文件的方式实现文件复制
        try (
                FileChannel inChannel = FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
                FileChannel outChannel = FileChannel.open(Paths.get(targetFilePath),
                        StandardOpenOption.READ,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.CREATE);
        ) {
            MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
            MappedByteBuffer outMap = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
            byte[] b = new byte[inMap.limit()];
            // 从磁盘文件中获取数据写入到b字节数组中
            inMap.get(b);
            // 将b字节数组中的数据写入到磁盘文件中
            outMap.put(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void copyFile3(String resourceFilePath, String targetFilePath) {
        // Channel-to-channel方式实现复制
        try (
                FileChannel inChannel = FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
                FileChannel outChannel = FileChannel.open(Paths.get(targetFilePath),
                        StandardOpenOption.READ,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.CREATE);
        ) {
            // inChannel.transferTo(0, inChannel.size(), outChannel);
            outChannel.transferFrom(inChannel, 0, inChannel.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4.2 SocketChannel、ServerSocketChannel 4.3 DatagramChannel

参考:https://blog.csdn.net/qq_38526573/article/details/89207100

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963434.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号