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驱动编程基础教程
版权声明 本书是免费电子书. 作者保留一切权利.但在保证本书完整性(包括版权声明.前言.正文内容.后记.以及作者的信息),并不增删.改变其中任何文字内容的前提下,欢迎任何读者 以任何形式(包括 ...
随机推荐
- android 学习过程中登陆失效的个人理解
今天在学习的过程中,要做登陆失效的功能,所以就找了些资料.好好看了一下.研究了一番,慢慢的做出来了! 比方:你在一个手机端登陆了账号,在另外的一个手机端也登陆了账号,此时.前一个手机端的账号会提示登陆 ...
- Codeforces Round #243 (Div. 2)——Sereja and Swaps
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24665103 题目链接 题意: 给定一个整数 ...
- c#代码获取web.config配置文件里面设置的 <compilation debug="true"节点
== 在我们的web.config文件中,有像下面的这样的一个配置, <system.web> <identity impersonate="true" user ...
- word如何让单页变横向
word作为图文排版用户最多的软件之一,其功能的强大自不必说,比如将某一页在版式排版上设置为横排方向.那么,应该如何才能设置为横排的纸张呢?请阅读下文! 工具/原料 Microsoft Office ...
- 在函数内部定义的函数 this 指向 undefined
在函数内部定义的函数 this 指向 undefined 以下这个 this 就是指向 undefined. 但在非 strict 模式下是指向 window <script> 'use ...
- Digester库使用总结
1.Digester是Apache软件基金会的Jakarta项目下的子Commons项目下的一个开源项目,Digester API包含3个包:org.apache.commons.digester,提 ...
- Redis的多线程
Redis是单线程内部机制,那么怎么实现并发?在单机上部署多个Redis实例.
- Android自动化测试中AccessibilityService获取控件信息(1)
Android自动化测试中AccessibilityService获取控件信息(1) 分类: android自动化测试2014-03-24 15:31 3455人阅读 评论(16) 收藏 举报 and ...
- ros建立ospf邻居的条件
Two routers do not become neighbors unless the following conditions are met. Two way communication b ...
- Spring Batch批处理以及编程模型
1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...