Channel的基本使用
来源:互联网 发布:淘宝卖家发货 具体流程 编辑:程序博客网 时间:2024/06/07 23:03
import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class ReadFile { public static void main(String[] args) throws IOException { /* FileInputStream fis=new FileInputStream("c:\\jdk\\b.txt"); FileOutputStream fos=new FileOutputStream("c:\\jdk\\d.txt"); FileChannel fco=fos.getChannel();//该channel只能写,读写文件用channel FileChannel fc=fis.getChannel();//该channel只能读 ByteBuffer bb=ByteBuffer.allocate(100);//channel必须和buffer联系 while(fc.read(bb)!=0) { bb.flip(); fco.write(bb); bb.clear(); } fis.close(); fos.close(); */ File f=new File("c:\\jdk\\b.txt"); RandomAccessFile raf=new RandomAccessFile(f, "rw"); raf.seek(f.length()); FileChannel rafc=raf.getChannel(); /** i. 使用InputStream获得的Channel可以映射,使用map时只能指定为READ_ONLY模式,不能指定为READ_WRITE和PRIVATE,否则会抛出运行时异常! ii. 使用OutputStream得到的Channel不可以映射!!并且OutputStream的Channel也只能write不能read! iii. 只有RandomAccessFile获取的Channel才能开启任意的这三种模式!*/ ByteBuffer bbb=rafc.map(FileChannel.MapMode.READ_ONLY, 0,f.length()); // rafc.position(f.length());//相当于seek System.out.println( raf.getFilePointer()); rafc.write(bbb); }}
阅读全文
0 0
- Channel的基本使用
- channel 标签的使用
- golang的channel使用
- golang channel 的使用
- golang channel的使用技巧
- golang channel的使用技巧
- 怎么使用golang的channel做广播
- 关于channel和chaincode的基础知识+基本测试
- golang使用之使用channel限制goroutine的数量
- Flume Channel Selectors使用
- Flume Channel Selectors使用
- Java拷贝文件的例子(使用channel实现)
- Golang -- 使用 Bufferd channel 实现 线程安全的 pool
- dede在channel标签的currentstyle愉快地使用全局变量
- 使用go channel实现一个简单的信号量
- Spring Integration channel中使用message-store的例子
- java NIO之Channel 通道 Charset字符集的使用
- # FLume 自定义Channel使用中遇到的问题(1)
- webstrom 新建php文件
- RCNN学习笔记(0):rcnn简介
- iBATIS SQL Maps之Result Map属性进阶。
- 部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi
- 参加中国大学生计算机设计大赛心得
- Channel的基本使用
- sparksql中的broadcast join和prestodb中的dynamic filter比较
- 计数排序-Counting Sort
- FZU 2181 快来买肉松饼(dfs)
- 51nod 1002 数塔取数问题(简单DP)
- 大四课程设计之基于RFID技术的考勤管理系统(三)数据库设计
- bootstrap 基础(一)
- Servlet总结
- 日历插件