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驱动编程基础教程
版权声明 本书是免费电子书. 作者保留一切权利.但在保证本书完整性(包括版权声明.前言.正文内容.后记.以及作者的信息),并不增删.改变其中任何文字内容的前提下,欢迎任何读者 以任何形式(包括 ...
随机推荐
- JNI学习笔记_C调用Java
一.笔记 1.C调用Java中的方法,参考jni.pdf pg97可以参考博文:http://blog.csdn.net/lhzjj/article/details/26470999步骤: a. 创建 ...
- repo学习笔记
1. 遍历所有的git仓库,并在每个仓库执行-c所指定的命令(被执行的命令不限于git命令,而是任何被系统支持的命令,比如:ls . pwd .cp 等 . $ repo forall -c &quo ...
- benthos 几个方便的帮助命令
benthos 的命令行帮助做的是比较方便的,基本上就是一个自包含的帮助文档 全部命令 benthos --help 查询系统支持的caches benthos -list-caches 说明 使用帮 ...
- FastAdmin 自己做的插件 SQL 有一个表没有生成成功
群里有群友问: 给插件建的install.sql 里有三个表,为啥会出现安装成功后没有错误提示,只生成了两个表的情况..这可能会是什么...原因 第一感觉和 FastAdmin 没有关系. 没生成表, ...
- FP-growth算法发现频繁项集(二)——发现频繁项集
上篇介绍了如何构建FP树,FP树的每条路径都满足最小支持度,我们需要做的是在一条路径上寻找到更多的关联关系. 抽取条件模式基 首先从FP树头指针表中的单个频繁元素项开始.对于每一个元素项,获得其对应的 ...
- php实现cookie加密解密
1.加密解密类 class Mcrypt { /** * 解密 * * @param string $encryptedText 已加密字符串 * @param string $key 密钥 * @r ...
- jmeter自动生成报告
从JMeter 3.0开始已支持自动生成动态报告,我们可以更容易根据生成的报告来完成我们的性能测试报告. 如何生成html测试报告 如果未生成结果文件(.jtl),可运行如下命令生成报告: jmete ...
- Spring Cloud 入门 之 Config 篇(六)
原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分 ...
- 监督学习(Supervised learning)
定义符号 m:训练样本的数目 n:特征的数量 x‘s:输入变/特征值 y‘s:输出变量/目标变量 (x,y):训练样本 ->(x(i),y(i)):训练集,第i个训练样本,i=1,2..,m 监 ...
- django orm 常用查询筛选
大于.大于等于 __gt 大于 __gte 大于等于 User.objects.filter(age__gt=10) // 查询年龄大于10岁的用户 User.objects.filter(age__ ...