博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5、NIO--通道的数据传输
阅读量:6991 次
发布时间:2019-06-27

本文共 3167 字,大约阅读时间需要 10 分钟。

 

实例1:

利用通完完成文件的复制(非直接缓冲区)

@Test    public void test4() throws IOException{                FileInputStream fis = new FileInputStream("d:\\a.jpg");        FileOutputStream fos = new FileOutputStream("d:\\b.jpg");                //获取通道        FileChannel inChannel=fis.getChannel();        FileChannel outChannel = fos.getChannel();                //指定大小的缓冲区        ByteBuffer buf = ByteBuffer.allocate(2048);                //将通道中的数据存入缓冲区        while(inChannel.read(buf) != -1){            //切换成读取数据的模式            buf.flip();                        //将缓冲区的数据写入通道            outChannel.write(buf);                        //清空缓冲区            buf.clear();                    }            //关闭        outChannel.close();        inChannel.close();        fos.close();        fis.close();            }

 

实例2:

使用直接缓冲区完成文件的复制(内存映射文件的方式)

等同于allocateDirect()方法

@Test    public void test5() throws IOException{                //open(Path path, OpenOption... options)         //读取        FileChannel inChannel = FileChannel.open(Paths.get("d:\\a.jpg"), StandardOpenOption.READ);        //写入        //StandardOpenOption.CREATE:存在就创建,不存在也进行创建(覆盖)        //StandardOpenOption.CREATE_NEW:存在就报错,不存在就创建       //此时需要三个权限   读 写 创建        FileChannel outChannel = FileChannel.open(Paths.get("d:\\c.jpg"),           StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);                //map(MapMode mode, long position, long size)        //分别代表操作方式,操作的位置,以及大小        MappedByteBuffer inMappedBuf = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());        MappedByteBuffer outMappedBuf = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());                //直接对缓冲区进行数据的读写        //定义字节进行读取数据        byte [] dst = new byte[inMappedBuf.limit()];        //读取数据        inMappedBuf.get(dst);        //存储数据        outMappedBuf.put(dst);                inChannel.close();        outChannel.close();    }

 

通道之间的数据传输

--transferForm():将数据从源通道传输到其他 Channel 中

( src, long position, long count)
src文件从
positoion位置到
count位置的数据获取
 

--transferTo():将数据从源通道传输到其他 Channel 中

transferTo(long position, long count, target)

文件从positioncount位置发送到target

 

注意:一下两种方法都是直接缓存区

public void test6() throws IOException{                FileChannel inChannel = FileChannel.open(Paths.get("d:\\a.jpg"), StandardOpenOption.READ);        FileChannel outChannel = FileChannel.open(Paths.get("d:\\d.jpg"),         StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);                inChannel.transferTo(0, inChannel.size(), outChannel);                //关闭        inChannel.close();        outChannel.close();            }

文件从inChannel中来发送到outChannel

 

@Test	public void test6() throws IOException{				FileChannel inChannel = FileChannel.open(Paths.get("d:\\a.jpg"),               StandardOpenOption.READ);		FileChannel outChannel = FileChannel.open(Paths.get("d:\\e.jpg"),             StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);				//inChannel.transferTo(0, inChannel.size(), outChannel);		outChannel.transferFrom(inChannel, 0, inChannel.size());		//关闭		inChannel.close();		outChannel.close();			}

转载于:https://www.cnblogs.com/Mrchengs/p/10822963.html

你可能感兴趣的文章
@property专题
查看>>
LNMP结合discuz的配置
查看>>
js中ul与li的使用
查看>>
实验二
查看>>
jquery.artDialog.source.js学习
查看>>
PDF去除签名
查看>>
socket
查看>>
date
查看>>
需求方如何选择优秀的项目外包团队?
查看>>
python笔记目录
查看>>
Java语法基础课 原码 反码 补码
查看>>
Nginx记录客户端POST过来的具体信息
查看>>
windows nginx安装与开机启动
查看>>
课后作业-阅读任务-阅读提问-3
查看>>
跨平台传值
查看>>
js点击标签时获取当前标签属性值
查看>>
C# Request.InputStream 读取输入流为空的原因处理
查看>>
golang 中map并发读写操作
查看>>
zabbix自动发现
查看>>
c++和c动态申请二维数组
查看>>