java使用RandomAccessFile类基于指针读写文件实例代码
作者:FlyingZCC 时间:2021-07-29 17:45:45
java API中提供了一个基于指针操作实现对文件随机访问操作的类,该类就是RandomAccessFile类,该类不同于其他很多基于流方式读写文件的类。它直接继承自Object。
public class RandomAccessFile extends Objectimplements DataOutput, DataInput, Closeable{...}
1.使用该类时可以指定对要操作文件的读写模式。
第一种模式是只读模式,第二种模式是读写模式。在创建该类实例时指定。
@Test
public void test01() throws IOException{
//读写模式
RandomAccessFile r=new RandomAccessFile(new File(""),"rw" );
//只读模式
//<span style="font-family:Arial, Helvetica, sans-serif;">RandomAccessFile r=new RandomAccessFile(new File(""),"r" );</span>
r.write(1);//写出一个字节,写出的是int值的低8位
r.close();
r.read();//每次读一个字节,填充到int的低八位
}
2.字节读取操作
(1)void write(int d):写出该int值的低8位,其他位舍弃
@Test
public void testWrite() throws IOException{
RandomAccessFile file=new RandomAccessFile(new File("emp.txt"),"rw");
file.write(97);//0110 0001
file.write(98);//0110 0010
file.write(99);//0110 0011
}
(2)int read():注意:该方法只会从文件中当前指针处读取一个byte(8位)的数据填充到int返回值的第八位,其他位用0填充。若该方法返回0,则代表读到文件末尾。
以上两种方法使用起来当然会很麻烦。
RandomAccessFile类中还封装了对8中基本数据类型的读写操作,使用起来会很方便。
分别为
readByte(), readShort(), readInt(), readLong(), readFloat(),readDouble(),readBoolean(),readChar()
返回值类型均为对应的基本数据类型。同时相应的也有这八中writeByte()...方法。这是在流式读写文件中所不具有的。
3.文件指针操作
RandomAccessFile类的所有读写操作均是基于指针的,指针会随着读或写的操作往后移动。同时也可以通过方法自由的操作指针的位置,以此更方便的读写文件。
常用方法
(1)long getFilePointer():该方法用于返回当前指针位置。默认读写操作时,指针的初始位置为文件的第一个字节处,即值为0
(2)void seek(long pos):该方法可以设定指针位置
(3)int skipBytes(int n):该方法可以跳过n个字节
/**
* RandomAccessFile:基于指针读写,总是在指针当前位置读写,无论读写,指针都会向后移动
* RandomAccessFile总是在指针当前位置进行读写字节,并且无论进行了读还是写一个字节后,
* 指针都会自动向后移动到下一个字节的位置
* 默认创建出来RandomAccessFile时,指针位置为0,即:文件的第一个字节的位置
* @author zc
*/
public class T13RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile(new File("emp.txt"),"rw");
int a=255;
//虽然能写入,但是在记事本中打开仍是乱码字符
raf.write(a>>>24);//11111111
raf.write(a>>>16);
raf.write(a>>>8);
raf.write(a);
//获取当前指针位置
long pos=raf.getFilePointer();
//!!注意,返回值是字节4,前面写入的四个字节对应从0--3字节
System.out.println("pos:"+pos);//4
//将int值分成四部分,写入
raf.writeInt(255);
System.out.println("pos:"+raf.getFilePointer());//8
raf.writeLong(255);//long八个字节
System.out.println("pos:"+raf.getFilePointer());//16
raf.writeFloat(8);//float四个字节
System.out.println("pos:"+raf.getFilePointer());//20
raf.writeDouble(12.1);//double八个字节
System.out.println("pos:"+raf.getFilePointer());//28
raf.write(new byte[]{1,1});
raf.writeBoolean(false);
raf.writeChar(1);
//此时已经写完,指针指向文件某位
System.out.println(raf.read());//-1
/*
*void seek(long pos)
*将指针移动到指定位置
* */
raf.seek(0);
System.out.println("point:"+raf.getFilePointer());//0
//读取刚刚写入的long值
raf.seek(8);
long l=raf.readLong();
System.out.println("读出的long值:"+l);//255
//读取刚刚写入的double值
raf.seek(20);
System.out.println(raf.readDouble());//12.1
raf.close();
}
}
来源:http://blog.csdn.net/kaishizhangcheng/article/details/52463850
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
生产消费者模式实现方式和线程安全问题代码示例
![](https://img.aspxhome.com/file/2023/0/60040_0s.png)
Java守护线程实例详解_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/5/60175_0s.png)
C语言实现自定义扫雷游戏(递归版)
![](https://img.aspxhome.com/file/2023/2/107382_0s.jpg)
springboot2.x使用Jsoup防XSS攻击的实现
![](https://img.aspxhome.com/file/2023/3/58733_0s.png)
java实现读取、删除文件夹下的文件
分析JVM源码之Thread.interrupt系统级别线程打断
Android带进度条的文件上传示例(使用AsyncTask异步任务)
![](https://img.aspxhome.com/file/2023/4/72624_0s.png)
Springboot 内部服务调用方式
![](https://img.aspxhome.com/file/2023/6/58426_0s.png)
java emoji表情存储的解决方法
Spring实现内置监听器
![](https://img.aspxhome.com/file/2023/8/63318_0s.png)
每日六道java新手入门面试题,通往自由的道路
Java class文件格式之常量池_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/2/66472_0s.jpg)
浅析Java线程的中断机制
Spring Boot 2.X 快速集成单元测试解析
![](https://img.aspxhome.com/file/2023/3/61503_0s.jpg)
解决Java提示正在尝试分配更低的访问权限问题
![](https://img.aspxhome.com/file/2023/9/58539_0s.png)
Android开发中Toast显示消息的方法小结
Java中LinkedList和ArrayList的效率分析
![](https://img.aspxhome.com/file/2023/9/61809_0s.png)
SpringMVC的执行过程浅析
![](https://img.aspxhome.com/file/2023/4/62074_0s.png)