FileChannel的用法
来源:互联网 发布:自动排料软件 编辑:程序博客网 时间:2024/06/05 06:46
package com.itbuluoge.nio;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class GetChannel {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {/*通过通道写入文件*/FileChannel fc=new FileOutputStream("data.txt").getChannel();fc.write(ByteBuffer.wrap("123456789".getBytes()));fc.close();/*读出文件*/fc=new FileInputStream("data.txt").getChannel();ByteBuffer buff=ByteBuffer.allocate(9);fc.read(buff);buff.flip();while(buff.hasRemaining()){System.out.print((char)buff.get());}}}
输出
这里有几个注意的点
第一,FileChannel类似与FileReader一样。
第二,FileChannel.read(ByteBuffer),将会读入一个ByteBuffer大小的数据块。
第三,在读取ByteBuffer的时候,需要设置ByteBuffer为读状态,也就是调用buff.flip();否则会无法读出数据。
0 0
- FileChannel的用法
- FileChannel类的简单用法
- FileChannel的深入理解
- java的FileChannel使用方法。
- FileChannel
- FileChannel
- FileChannel
- FileChannel
- Java 复制大文件方式FileChannel 用法
- 合并文件的FileChannel方式
- Java的NIO之FileChannel
- FileChannel实现的简单断点复制
- FileChannel 传输(zero-copy 的基础)
- FileChannel类的理解和使用(java.nio.channels.FileChannel)
- Java6学习笔记70—— FileChannel的学习
- 使用NIO的FileChannel和ByteBuffer高效读取文件
- JAVA中IO和NIO的详解分析,FileChannel
- 利用FileChannel完成文件的读、写、复制
- java中的matches()方法
- 开始了
- 一对多hibernate 添加数据(二)
- iis问题
- xml-schema仅含有文本及案例
- FileChannel的用法
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- memcached介绍及原理
- CGI的基本原理
- uva--10474+水题
- 通过ApplicationContextAware获取bean
- xml-schema空元素约束及案例
- 《算法竞赛入门经典(第2版)》代码 Chapter 3
- hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离