- 一、FileChannel方法说明
- 1.1、FileChannel工作模式
- 1.2、FileChannel获取
- 1.3、FileChannel读取
- 1.4、FileChannel写入
- 1.5、FileChannel关闭
- 1.6、FileChannel位置
- 1.7、FileChannel大小
- 二、两个 Channel 传输数据示例(一)
- 三、两个 Channel 传输数据示例,传输数据 >2G(二)
- FileChannel 只能工作在阻塞模式下。
- 不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法。
- 通过 FileInputStream 获取的 channel 只能读。
- 通过 FileOutputStream 获取的 channel 只能写。
- 通过 RandomAccessFile 是否能读写根据构造 RandomAccessFile 时的读写模式决定。
-
从 channel 读取数据填充 ByteBuffer,返回值表示读到了多少字节,-1 表示到达了文件的末尾。
int readBytes = channel.read(buffer);
-
在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel
ByteBuffer buffer = ...; buffer.put(...); // 存入数据 buffer.flip(); // 切换读模式 while(buffer.hasRemaining()) { channel.write(buffer); }
- channel必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法。
-
获取当前位置
long pos = channel.position();
-
设置当前位置
long newPos = ...; channel.position(newPos);
-
设置当前位置时,如果设置为文件的末尾
(1)、这时读取会返回 -1
(2)、这时写入,会追加内容,但要注意如果 position 超过了文件末尾,再写入时在新内容和原末尾之间会有空洞(00)
- 使用 size 方法获取文件的大小
-
创建一个from.txt文件,内容如下:
-
创建一个文件内容为空的to.txt文件,如下:
-
示例代码
package com.example.nettytest.nio.day2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class TestFileChannelTransferTo { public static void main(String[] args) { transferTo1(); } //通过FileChannel传输数据(方式一) public static void transferTo1(){ try (FileChannel from = new FileInputStream("file/from.txt").getChannel(); FileChannel to = new FileOutputStream("file/to.txt").getChannel(); ) { from.transferTo(0,from.size(),to); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } -
运行代码后查看to.txt文件中的内容,如下图:
-
创建一个from.txt文件,内容如下:
-
创建一个文件内容为空的to.txt文件,如下:
-
示例代码
package com.example.nettytest.nio.day2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class TestFileChannelTransferTo { public static void main(String[] args) { transferTo2(); } //通过FileChannel传输数据(方式二),传输数据 >2G public static void transferTo2(){ try (FileChannel from = new FileInputStream("file/from.txt").getChannel(); FileChannel to = new FileOutputStream("file/to.txt").getChannel(); ) { for (long left =from.size(); left >0;){ System.out.println("position:" + (from.size() - left) + " left:" + left); //transferTo 数据传输方法 left -= from.transferTo((from.size() - left), left, to); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } -
运行代码后查看to.txt文件中的内容,如下图:



