Java NIO深入理解通道之间数据传输
2018-02-16 23:08
591 查看
在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(通道)传输到另外一个channel。
通道之间数据传输方法:transferFrom(),transferTo()
transferFrom()
FileChannel的transferFrom()方法可以将数据从源通道传输到FileChannel中。
eg:
方法的输入参数position表示从position处开始向目标文件写入数据,count表示最多传输的字节数。
如果源通道的剩余空间小于count个字节,则所传输的字节数要小于请求的字节数。
此外要注意,在SoketChannel的实现中,SocketChannel只会传输此刻准备好的数据(可能不足count字节)。
因此,SocketChannel可能不会将请求的所有数据(count个字节)全部传输到FileChannel中。
transferTo()
transferTo()方法将数据从FileChannel传输到其他的Channel中。
eg:
这两个实例除了调用方法的FileChannel对象不一样外,其他的都一样。
上面所说的关于SocketChannel的问题在transferTo()方法中同样存在。
SocketChannel会一直传输数据直到目标buffer被填满。
通道之间数据传输方法:transferFrom(),transferTo()
transferFrom()
FileChannel的transferFrom()方法可以将数据从源通道传输到FileChannel中。
eg:
package com.lanhuigu.nio.channel; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * FileChannel中transferFrom源码: * public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException; * FileChannel的transferFrom()方法可以将数据从源通道传输到FileChannel中。 */ public class TestTransferFrom { /** * 文件复制: 将fromFile.txt文件内容复制到toFile.txt中 */ public static void main(String[] args) { try ( // 指定文件创建文件输入流 FileInputStream fromFile = new FileInputStream("C:\\mycode\\fromFile.txt"); // 指定文件创建文件输出流 FileOutputStream toFile = new FileOutputStream("C:\\mycode\\toFile.txt"); // 获取通道 FileChannel fromChannel = fromFile.getChannel(); FileChannel toChannel = toFile.getChannel()) { // 定义position的位置为初始位置0 long position = 0; // 获取文件总的字节数 long count = fromChannel.size(); // 从fromChannel通道缓冲区position位置开始读取count字节数写入到目标通道toChannel中 toChannel.transferFrom(fromChannel, position, count); } catch (IOException e) { e.printStackTrace(); } } }
方法的输入参数position表示从position处开始向目标文件写入数据,count表示最多传输的字节数。
如果源通道的剩余空间小于count个字节,则所传输的字节数要小于请求的字节数。
此外要注意,在SoketChannel的实现中,SocketChannel只会传输此刻准备好的数据(可能不足count字节)。
因此,SocketChannel可能不会将请求的所有数据(count个字节)全部传输到FileChannel中。
transferTo()
transferTo()方法将数据从FileChannel传输到其他的Channel中。
eg:
package com.lanhuigu.nio.channel; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * FileChannel的transferTo源码: * public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException; * transferTo()方法将数据从FileChannel传输到其他的Channel中。 */ public class TestTransferTo { /** * 文件复制: 将fromFile.txt文件内容复制到toFile.txt中 */ public static void main(String[] args) { try ( // 指定文件创建文件输入流 FileInputStream fromFile = new FileInputStream("C:\\mycode\\fromFile.txt"); // 指定文件创建文件输出流 FileOutputStream toFile = new FileOutputStream("C:\\mycode\\toFile.txt"); // 获取通道 FileChannel fromChannel = fromFile.getChannel(); FileChannel toChannel = toFile.getChannel()) { // 定义position的位置为初始位置0 long position = 0; // 获取文件总的字节数 long count = fromChannel.size(); // 从fromChannel通道缓冲区position位置开始读取count字节数写入到目标通道toChannel中 fromChannel.transferTo(position, count, toChannel); } catch (IOException e) { e.printStackTrace(); } } }
这两个实例除了调用方法的FileChannel对象不一样外,其他的都一样。
上面所说的关于SocketChannel的问题在transferTo()方法中同样存在。
SocketChannel会一直传输数据直到目标buffer被填满。
相关文章推荐
- Java NIO 通道之间的数据传输
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
- NIO边看边记 之 通道之间的数据传输(五)
- Java的NIO之不同channel之间传输数据
- nio 本地文件操作之通道之间的数据传输
- 五、Nio之通道之间的数据传输
- Java NIO深入理解Channel(通道)
- JAVA-4NIO之Channel之间的数据传输方法
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- 深入理解java NIO
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO 通道和通道之间转换
- java与c之间用socket传输数据
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO系列教程(五) 通道之间的数据传输
- Java NIO系列教程(五) 通道之间的数据传输