Java_RandomAccessFile

本文探讨了硬盘读写效率的优化策略,重点讲解了块读写的优势,并通过实例演示了如何使用RandomAccessFile进行高效的数据读写,包括读取字节、跳过字节、设置指针位置等关键操作。

1、由于硬盘的物理性,导致随机读写的效率很低,因此我们推荐“块读写”;块读写的效率还是很不错的。
随机读写:是自己读写数据;
块读写:一次一组自己读写数据;
所有若想提高读写效率我们需要提高每次读写的数据量,减少实际读写此时来达到优化的性能。
1byte 8位2金子
1kb 1024byte
1mb 1024kb
1gb 1024mb

RandomAccessFile raf_read = new RandomAccessFile("C:Users/demo","r");
RandomAccessFile raf_write = new RandomFile("C:/Users/demo1","rw");
byte[] bytes = new byte[1024*10];
int len=-1;
System.out.println("正在复制...");
long time1 = System.currentTimeMillis();
while((len=raf_read.read(bytes))!=-1){
	raf_write.write(byte,0,len);//每次写入byte个字符从0开始到len个
}
long time2 = System.currentTimeMillis();
System.out.println("块复制后消耗的时间"+(time2-time1));
raf_write.close();
raf_read.close();

2、从文件中读取字节

RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
/*
*RandomAccessFile提供了int read();
1.从文件中读取一个字节并以int形式返回
2.若返回值为-1则表示读取到了文件末尾
*/
int d = raf.read();
System.out.println(d);
d = raf.read();
System.out.println(d);//表示读到末尾
raf.close();

3、从文件中读取字节

RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
/*
* RandomAccessFile提供了int read();
* 1、从文件中读取一个字节并以int形式返回
* 2、若返回值为-1则表示读取到了文件末尾
*/
int d = raf.read();
System.out.println(d);
d = raf.read();
System.out.println(d);//表示读到了末尾
raf.close();

4、读取文件内容

RandomAccessFile raf = new RandomAccessFile("raf01.txt","rw");
int len = -1;//保存读取到的每一个字节数据
while((len=raf.read())!=-1){//数据读取-1时则读到文件的末尾
	System.out.println((char)len);
}

5、利用RAF读取中文数据

RandomAccessFile raf = new RandomAccessFile("raf02.txt","r");
byte[] b = new byte[100];
//读取100个字节并存放在b字节数组中,返回的是实际读到的字节数量
int len = raf.read(b);
/*读取出来的字节要转化为字符串*/
String str = new String(b,0,len,"UTF-8");
System.out.println(str);
raf.close();//关闭资源

6、RAF提供了一个方法字节跳过skipBytes(int n);
n表示跳过n个字节

RandomAccessFile raf = new RandomAccessFile("raf);
byte[] b = new byte[5];
	raf.skipBytes(5);
	int len = raf.read(b);
	String str = new String(b,0,len,"UTF-8");
	System.out.println(str);
	raf.close();

7、设置指针位置使用seek,可以利用这个写入中文

RandomAccessFile  raf = new RandomAccessFile("raf02.txt","rw");
		String str= "世界上唯一不能复制的是人生";
		raf.seek(39);
		raf.write(str.getBytes("UTF-8"));
		System.out.println("写入完毕!");
		raf.close();

8、String提供将字符串转化为字节的方法,byte[] getByte()

String str = "world";
System.out.println(str.length());
byte[] n = str.getBytes();//将字符串转化为字节
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
//获取指针位置(游标位置)RAF提供了指针位置的方法getFilePointer()
long p = raf.getFilePointer();
System.out.println("写入之前指针位置"+p);
raf.seek(4);

raf.write(n);
p = raf.getFilePointer();
System.out.println("写入之后指针位置"+p);
System.out.println("写入完毕");
raf.close();

9、RAF在数据库底层操作

/**
* 按照系统默认字符集转换(不推荐,存在平台差异)
* byte[] getByte(String scn)
* 按照给定的字符集转换,字符集的名字不区分大小写,
* GBK:国际编码,中文2字符
* UTF-8::Unicode的自己,也称万国码,中文3字节
* ISO8859-1:欧洲字符集,不支持中文
*/
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
raf.seek(10);
String str = ",我很好";
byte[] b = str.getByte("UTF-8");
raf.write(b);
System.out.println("输入完毕!");
raf.close();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值