随机读取文件----RandomAccessFile类
文章目录
- 随机读取文件----RandomAccessFile类
-
-
- 前言
- 主要方法
- 案例演示
-
-
- 使用RandomAccessFile类写入数据
-
- 案例代码
- 运行结果
- 使用RandomAccessFile类读取数据
-
- 案例源码
- 运行结果
-
- 总结
-
前言
File类可以提供对于文件本身的操作,而要对文件内容进行操作的话,可以用到RandomAccessFile类,此类属于随机读取类,可以随机读取一个文件中指定位置的数据。
主要方法
序号 | 方法 | 类型 | 描述 |
---|---|---|---|
1 | public RandomAccessFile(File file, String mode) throws FileNotFoundException | 构造 | 接收File类的对象,指定操作路径,但是在设置时需要设置模式,r为只读,w为只写,rw为读写 |
2 | public RandomAccessFile(String name, String mode) throws FileNotFoundException | 构造 | 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径 |
3 | public void close() throws IOException | 普通 | 关闭操作 |
4 | public int read(byte[] b) throws IOException | 普通 | 将内容读取到一个byte数组中 |
5 | public final byte readByte() throws IOException | 普通 | 读取一个字节 |
6 | public final int readInt() throws IOException | 普通 | 从文件中读取整型数据 |
7 | public void seek(long pos) throws IOException | 普通 | 设置读指针的位置 |
8 | public final void writeBytes(String s) throws IOException | 普通 | 将一个字符串写入到文件中,按字节的方式处理 |
9 | public final void writeInt(int v) throws IOException | 普通 | 将一个int型数据写入到文件,长度为4位 |
10 | public int skipBytes(int n) throws IOException | 普通 | 将指针跳过指定的字节 |
案例演示
使用RandomAccessFile类写入数据
案例代码
package chapter_twelve;
import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo01 {
public static void main(String[] args) throws Exception{
File file = new File("D:" + File.separator + "text.txt"); //实例化File类对象文件
if(file.exists()){ //若此文件已经存在,将此文件进行删除
file.delete();
}
file.createNewFile(); //调用createNewFile方法创建新的文件
RandomAccessFile randomAccessFile =
new RandomAccessFile(file,"rw"); //实例化RandomAccessFile类对象,以读写的方式进行
String name = "zhangsan"; //声明字符串name的值为zhangsan
int age = 30; //声明int型数据的值为30
randomAccessFile.writeBytes(name); //以一个字符串写入文件中,按字节方式处理
randomAccessFile.writeInt(age); //将一个int型数据写入文件,长度为4位
name = "lisi "; //设置name的值为lisi
age = 20; //年龄为20
randomAccessFile.writeBytes(name); //写入文件
randomAccessFile.writeInt(age);
name = "wangwu "; //设置name的值为wangwu
age = 21; //年龄为21
randomAccessFile.writeBytes(name); //写入文件
randomAccessFile.writeInt(age);
randomAccessFile.close(); //关闭RandomAccessFile类的操作
}
}
运行结果
D盘会创建一个text.txt文件,并将数据以字节的方式写入文件,如图
使用RandomAccessFile类读取数据
案例源码
package chapter_twelve;
import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo02 {
public static void main(String[] args) throws Exception{
File file = new File("D:" + File.separator + "text.txt"); //实例化文件类对象file
if(!file.exists()){ //若此文件类对象不存在,则程序直接退出
System.exit(0);
}
RandomAccessFile randomAccessFile =
new RandomAccessFile(file,"r"); //实例化RandomAccessFile类对象,以只读的方式对file文件操作
byte[] name = new byte[8]; //定义byte数组,用来存放读取的信息
randomAccessFile.skipBytes(12); //跳过前12个字节,暂时忽略第一个人的信息
for (int i = 0; i < name.length; i++){ //循环读取出第二个人的姓名,以byte数组存放
name[i] = randomAccessFile.readByte();
}
System.out.println("第二个学生的信息---->姓名:" + new String(name)
+ "\t年龄:" + randomAccessFile.readInt()); //输出第二个人的信息
randomAccessFile.seek(0); //指针回到文件的开头
for (int i = 0; i < name.length; i++){
name[i] = randomAccessFile.readByte();
}
System.out.println("第一个学生的信息---->姓名:" + new String(name)
+ "\t年龄:" + randomAccessFile.readInt()); //输出第一个人的信息
randomAccessFile.skipBytes(12);
for (int i = 0; i < name.length; i++){
name[i] = randomAccessFile.readByte();
}
System.out.println("第三个学生的信息---->姓名:" + new String(name)
+ "\t年龄:" + randomAccessFile.readInt()); //输出第三个人的信息
randomAccessFile.close(); //关闭RandomAccessFile类对象
}
}
运行结果
第二个学生的信息---->姓名:lisi 年龄:20
第一个学生的信息---->姓名:zhangsan 年龄:30
第三个学生的信息---->姓名:wangwu 年龄:21
总结
随机读写流可以实现对文件内容的操作,但是过于复杂,所以一般情况下操作文件会使用字节流或字符流,本类小伙伴们理解会使用即可哦,哈哈哈,充实的天天,吃饭!!!(小伙伴们有疑问欢迎评论留言哦)