当前位置:网站首页>Buffer的只读模式
Buffer的只读模式
2022-07-27 16:00:00 【StrugglingXuYang】
package com.tdkj.DataApi.nio;
import java.nio.ByteBuffer;
/** * @author hxy * @version 1.0 * @date 2021-03-11 10:56 */
public class ReadOnlyBuffer {
public static void main(String[] args) {
//buffer只读模式
//创建一个buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
for (int i = 0; i <64 ; i++) {
byteBuffer.put((byte)i);
}
//读取
byteBuffer.flip();
//将该buff设置为只读
ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
while (readOnlyBuffer.hasRemaining()){
//hasRemaining 确定下一位还有没有数据
System.out.println(readOnlyBuffer.get()); //get会是 position指针自动+1
}
//readOnlyBuffer.putInt(1);
//设置为只读后那么就不能够写入了 否则会报该错误
//java.nio.ReadOnlyBufferException
}
}
直接修改通道内的数据
package com.tdkj.DataApi.nio;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/** * @author hxy * @version 1.0 * @date 2021-03-11 11:07 */
public class MappedByteBufferTest {
//MappedByteBuffer可以让文件直接在内存修改
//操作系统不需要拷贝一次 内存指堆外内存
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("D://file.txt", "rw");
//获取对应的通道
FileChannel channel = randomAccessFile.getChannel();
/** * 参数1 FileChannel.MapMode.READ_WRITE 使用读写模式 * 参数2 0 可以直接修改的起始位置 * 参数3(size) 5 是映射到内存的大小(不是索引的位置),即 从文件中将多少个字节映射到内存 * 也就是可以直接修改的范围就是0,1,2,3,4 5个字节 * 实际类型为 DirectByteBuffer **/
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE,0,20);
mappedByteBuffer.put(0,(byte)'a');
mappedByteBuffer.put(1,(byte)'b');
mappedByteBuffer.put(4,(byte)'c');
mappedByteBuffer.put(7,(byte)'c');
mappedByteBuffer.put(8,(byte)'c');
mappedByteBuffer.put(9,(byte)'c');
//20的这个位置上是不能修改的 也就是 0-20 但20指的是可修改的字节位数 而不是下标
//mappedByteBuffer.put(20,(byte)'c');
//如果文件中存在中文 那么也会修改 不过会变成乱码
randomAccessFile.close();
}
}
边栏推荐
- 美团二面:为什么Redis会有哨兵?
- CPU introduction
- [Southwest University] information sharing of postgraduate entrance examination and re examination
- 多线程导入数据并生成错误文件用redis存储
- MySQL adds users and grants query only permission
- ts学习笔记-interface
- vue使用keep-alive实现页面缓存
- 快速获取网站媒体资源方法
- [introduction to database system (Wang Shan)] Chapter 5 - database integrity
- Bubble sorting in JS
猜你喜欢

WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl

WPF做登陆界面

Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination

工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线

How difficult the interview is! I was forced to survive the six rounds of interview of ant financial! Almost out (interview resumption)

机器学习之评价指标(一)——回归评价指标

The latest advanced interview questions for big factories are necessary

使用分布式框架WCF出现的BUG记录

WPF makes login interface

Count the six weapons of the domestic interface cooperation platform!
随机推荐
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
Knowledge dry goods: basic storage service novice Experience Camp
MySQL solves the problem of insert failure caused by duplicate unique indexes
Kubernetes 1.24 high availability cluster binary deployment
vim的配置及基础使用
org.gradle.api.UncheckedIOException: Could not load properties for module ‘gradle-kotlin-dsl‘ from C
ts学习笔记-interface
Getting started with shell programming base
工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线
hutool 字符串工具类
Telecommuting can be easily realized in only three steps
In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information
【学习笔记】Redis中有序集合zset的实现原理——跳表
2022 safety officer-c certificate special operation certificate examination question bank and answers
Golang worker pool
机器学习——概念理解之IoU
多线程导入数据并生成错误文件用redis存储
Multi thread implementation loop
mysql解决唯一索引重复导致的插入失败问题
Golang Chan implements mutual exclusion