Java的NIO之不同channel之间传输数据
2016-12-05 13:21
357 查看
5.不同通道channel之间传输数据
在Java的NIO中,如果两个通道中有一个是FileChannel,那么我们可以直接将数据从一个channel传输到另外一个channel中。两个通道之间传输数据的方式有两种,分别是:- transferFrom()
- transferTo()
5.1 transferFrom()
FileChannel 的transferFrom()方法可以将数据从源通道传输到FileChannel中(这个方法在JDK文档中解释为将字节从给定的可读取字节通道传输到此通道的文件中)。下面是一个简单的例子:RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw"); FileChannel fromChannel = fromFile.getChannel();//获取source的通道; RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw"); FileChannel toChannel = toFile.getChannel();//获取dest的通道; long position = 0; long count = fromChannel.size(); long result = toChannel.transferFrom(fromChannel, position, count); LogUtil.log_debug(""+result);
方法的输入参数
- position表示从position处开始向目标文件写入数据,
- count表示最多传输的字节数。如果源通道的剩余空间小于count个字节,则所传输的字节数要小于请求的字节数。
此外要注意,在SoketChannel的实现中,SocketChannel只会传输此刻准备好的数据(可能不足count字节)。因此,SocketChannel可能不会将请求的所有数据(count个字节)全部传输到FileChannel中。
5.2 transferTo()方法
transferTo()方法将数据从FileChannel传输到其他的channel中。下面是一个简单的例子:RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw"); FileChannel fromChannel = fromFile.getChannel();//获取source的通道; RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw"); FileChannel toChannel = toFile.getChannel();//获取dest的通道; long position = 0; long count = fromChannel.size(); long result = fromChannel.transferTo(position, count, toChannel); LogUtil.log_debug(""+result);
观察一下就可以发现,其实这两个例子其实非常的相似,如果仅仅从文件IO的角度来看,就是两个文件之间的数据复制;
上面所说的关于SocketChannel的问题在transferTo()方法中同样存在。SocketChannel会一直传输数据直到目标buffer被填满。
相关文章推荐
- JAVA-4NIO之Channel之间的数据传输方法
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
- Java NIO深入理解通道之间数据传输
- NIO(2) Channel之间数据传输
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- Java NIO 通道之间的数据传输
- Java NIO系列教程(二) Channel原文链接 作者:Jakob Jenkov 译者:airu 校对:丁一 Java NIO的通道类似流,但又有些不同: 既可以从通
- JAVA NIO实现服务端与客户端简单数据传输 JAVA NIO 之一
- scp 在不同主机之间数据传输
- java的nio之:java的nio系列教程之channel的数据交换
- 【WP开发】不同客户端之间传输加密数据
- 关于java不同类之间的数据共享
- 不同数据库之间数据传输
- Java不同数据类型之间的转换
- Java之NIO传输数据
- 网络-Netty(一)-NIO概念Channel是一个对象,可以通过它读取和写入数据。可以把它看做IO中的流。但是它和流相比还有一些不同:
- JSon与java之间数据传输的方式
- 一台MySql服务器不同数据库之间数据同步_解决方案(Java)
- scp 在不同主机之间数据传输