当前位置:网站首页>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();
}
}
边栏推荐
- ts学习笔记-interface
- vue使用keep-alive实现页面缓存
- Es query limit 10000 data solutions
- Error launching IDEA
- Understand │ what is cross domain? How to solve cross domain problems?
- PostgreSQL 14 支持winserver2022吗?
- Oracle 11g database installation tutorial
- [introduction to database system (Wang Shan)] Chapter 5 - database integrity
- 年终总结模板
- [introduction to database system (Wang Shan)] Chapter 4 - Database Security
猜你喜欢

Evaluation index of machine learning (II) -- classification evaluation index

知物由学 | 关联图分析在反作弊业务中的应用

知物由学 | 从0到1搭建实时反外挂机制,多维度补充手游攻防力

【Codeforces】 A. Computer Game

7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制

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

机器学习——概念理解之IoU
知物由学 | 小游戏的安全风险在哪里?

WPF makes login interface

Understand │ what is cross domain? How to solve cross domain problems?
随机推荐
wallys/DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power.
2022 safety officer-c certificate special operation certificate examination question bank and answers
fragmentTransaction.replace第二个参数报错
年终总结模板
Evaluation index of machine learning (I) -- regression evaluation index
Golang concurrent cache breakdown or merge request
I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
Hutool string utility class
【学习笔记】Redis中有序集合zset的实现原理——跳表
Convolutional neural network -- from r-cnn, fast r-cnn to fast r-cnn, mask r-cnn
Understand JVM language
WPF做登陆界面
hutool- 数字计算
Wechat applet realizes location map display and introduces map map without navigation
Salesforce dynamic dashboard dynamic reports, limitations and Solutions
How to improve the security of Android applications?
In the first week of June, risk control of e-shield business paid attention to 15 institutions such as New Oriental XRS, which were fined
Personal understanding of convolution calculation process of convolution neural network
知物由学 | 易盾自研文本实时聚类技术,一网打尽社交网络中的同类有害内容
浅论分布式训练中的recompute机制