J2SE 8的输入输出--缓冲
FileChannel带缓冲
//1. read the point location
FileChannel channelRead = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
channelRead.read(buffer);
buffer.flip(); //flip()方法将界限设置到当前位置,位置复位为0,为读入做好准备
while(buffer.hasRemaining()){
System.out.print((char)buffer.get());
}
System.out.println();
channelRead.close();
//重置为空状态;它并不改变缓冲区中的任何数据元素,而是仅仅将 limit 设为容量的值,并把 position 设回 0
buffer.clear();
//2. write the point location
FileChannel channelWrite = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);
//指定容量的缓冲区
// buffer = ByteBuffer.allocate(1024);
// buffer.put("测似乎".getBytes());
//对给定数组的缓冲区
buffer = ByteBuffer.wrap("abcdefg Java开发手册".getBytes());
buffer.limit("abcdefg Java开发手册".getBytes().length); //Sets this buffer's limit
while(buffer.hasRemaining()){
channelWrite.write(buffer);
}
channelWrite.close();
ByteBuffer
//1. ByteBuffer
//(1) 读取buffer
ByteBuffer byteBuffer = ByteBuffer.wrap("111 222 333 444 555 666 777".getBytes());
// byteBuffer.flip(); //flip()方法将界限设置到当前位置,位置复位为0,为读入做好准备
while(byteBuffer.hasRemaining()){
System.out.print((char)byteBuffer.get());
}
System.out.println();
//重新读取缓冲区
byteBuffer.rewind();
while(byteBuffer.hasRemaining()){
System.out.print((char)byteBuffer.get());
}
System.out.println();
byteBuffer.rewind();
byteBuffer.position(12); //返回缓冲区的位置
byteBuffer.compact(); //读取position指向的位置和limit直接的值; 丢弃已经释放的数据,保留未释放的数据,并使缓冲区对重新填充容量准备就绪
while(byteBuffer.hasRemaining()){
System.out.print((char)byteBuffer.get());
}
System.out.println();
//(2) 写buffer
byteBuffer = ByteBuffer.wrap("abcdefg Java开发手册".getBytes());
byteBuffer.limit("abcdefg Java开发手册".getBytes().length); //Sets this buffer's limit
while(byteBuffer.hasRemaining()){
System.out.println(byteBuffer.get());
}
//(3) 转换
byteBuffer.clear();
CharBuffer asCharBuffer = byteBuffer.asCharBuffer();
ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
System.out.println();
CharBuffer
//(1) 读
CharBuffer charBuffer = CharBuffer.wrap("abcdefg Java开发手册".toCharArray());
System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());
System.out.println(charBuffer.toString()); //返回包含此缓冲区中字符的字符串
charBuffer.append("第一期面授培训大纲", charBuffer.position(), charBuffer.position()+"第一期面授培训大纲".length());
System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());
int limit = charBuffer.limit();
charBuffer.flip();
charBuffer.limit(limit);
System.out.println(charBuffer.toString());
System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());
//(2) 写
//(3) 转换
charBuffer.clear();
CharBuffer asReadOnlyBuffer2 = charBuffer.asReadOnlyBuffer();
Channel lock/tryLock
//3. 锁机制
//1). 对于一个只读文件通过任意方式加锁时会报NonWritableChannelException异常
//2). 无参lock()默认为独占锁,不会报NonReadableChannelException异常,因为独占就是为了写
//3). 有参lock()为共享锁,所谓的共享也只能读共享,写是独占的,共享锁控制的代码只能是读操作,当有写冲突时会报NonWritableChannelException异常
//(1) 锁定文件, 默认为独占锁, 阻塞的方法, 当文件锁不可用时,当前进程会被挂起
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);
FileLock lock = channel.lock();
){
ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());
channel.write(sendBuffer);
lock.release();
}
//(2) 锁定文件, 默认为独占锁, 非阻塞的方法, 当文件锁不可用时,tryLock()会得到null值
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);){
FileLock lock = null;
try{
do{
lock = channel.tryLock();
}while(null==lock);
ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());
channel.write(sendBuffer);
lock.release();
}finally {
if(null!=lock){
lock.close();
}
}
}
//release
//(3) 锁定部分文件
//shared = true, 表示为共享锁, 多个进程可以读入, 阻止其它获得独占的锁
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);
FileLock lock = channel.lock(2, 3, false);
){
ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());
channel.write(sendBuffer);
}
//(4) 锁定部分文件
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);){
FileLock lock = null;
try{
do{
lock = channel.tryLock(2, 3, false);
}while(null==lock);
ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());
channel.write(sendBuffer);
}finally {
if(null!=lock){
lock.close();
}
}
}
J2SE 8的输入输出--缓冲的更多相关文章
- C和指针 第十五章 输入输出缓冲
对于C,所有的I/O操作都只是简单的从程序移进或移出字节,这种字节流便成为流(stream),我们需要关心的只是创建正确的输出字节数据,以及正确的输入读取数据,特定的I/O设备细节都是对程序隐藏的. ...
- java IO之 File类+字节流 (输入输出 缓冲流 异常处理)
1. File类
- J2SE 8的输入输出--Path/Paths File/Files; FileSystems 类的用法
Path的简单用法 //1. Path 正常用法 Path path = Paths.get("src/main/resource/zip"); logger.debug(path ...
- J2SE 8的输入输出--序列化
1. 普通序列化 implements Serializable 继承Serializable接口 class Employee implements Serializable { private S ...
- J2SE 8的输入输出--读取/写入文本文件和读取/写入二进制数据
读取/写入文本文件 // 1. 文本输入 // (1) 短小文本直接转入字符串 String string = new String(Files.readAllBytes(Paths.get(&quo ...
- java中的缓冲流!
package cn.zhozuohou; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- 【Go语言】I/O专题
本文目录 1.bytes包:字节切片.Buffer和Reader 1_1.字节切片处理函数 1_1_1.基本处理函数 1_1_2.字节切片比较函数 1_1_3.字节切片前后缀检查函数 1_1_4.字节 ...
- A trip through the graphics pipeline 2011 Part 10(翻译)
之前的几篇翻译都烂尾了,这篇希望....能好些,恩,还有往昔呢. ------------------------------------------------------------- primi ...
- (转)Windows驱动编程基础教程
版权声明 本书是免费电子书. 作者保留一切权利.但在保证本书完整性(包括版权声明.前言.正文内容.后记.以及作者的信息),并不增删.改变其中任何文字内容的前提下,欢迎任何读者 以任何形式(包括 ...
随机推荐
- test20180829
试题限制均为128MB,1Sec 总分150. 试题一 A题 问题描述: 小A得到了一棵美丽的有根树.这棵树由n个节点以及n - 1条有向边构成,每条边都从父亲节点指向儿子节点,保证除了根节点以外的每 ...
- sql server merge into 与update 批量更新1 百万测试数据的性能比较
1. 1百万的测试数据的生成 declare @index int; begin set @index=0; while @index<1000000 begin insert int ...
- Mysql ON子句和USING子句
Mysql ON子句和USING子句 Mysql 中联接SQL语句中,ON子句的语法格式为:table1.column_name = table2.column_name. 当模式设计对联接表的列 ...
- node express 返回json object
web 开发的过程中我们经常需要返回对象的json 格式,使用node express 是比较简单的, 1.node express 基础网站的创建 比较简单,以前的文章有 2.编写对象并导出对象 / ...
- Openssl将crt证书和key私钥合成pfx证书
下载OpenSSL地址:http://slproweb.com/products/Win32OpenSSL.html 下载安装openssl 选择对应OpenSSL版本进行下载下载. 运行安装程序Wi ...
- tomcat catalina.out切割脚本
shell脚本catalina.out 切割脚本...每天23.30切割.删除七天之前的日志这里3个tomcat实例(1)拷贝日志文件(2)清空日志文件*只能清空如果删除tomcat不重启不会生成新的 ...
- C# ObjectArx AutoCAD二次开发(转帖)
http://www.cnblogs.com/houlinbo/p/3325898.html 1.开发基本资料准备 用Vs2010进行Autocad 2010开发,首先下载ObjectArx 2010 ...
- Windows平台下的线程同步
引子: 这几天在写一个windows phone平台上的service,由于Windows phone 的内核是基于Windows NT的,这也意味着写Windows Phone的Service代码与 ...
- typedef与前向声明
a.h: typedef struct my_struct { }my_struct_typedef; b.h: struct my_struct; typedef my_struct my_stru ...
- 【Oracle学习笔记-5--】集合操作之union,intersect和minus操作
--union并操作 select e.employee_id,e.last_name from hr.employees e where e.last_name like 'C%' union se ...