随机读取文件----RandomAccessFile类
文章目录
随机读取文件----RandomAccessFile类前言主要方法案例演示使用RandomAccessFile类写入数据案例代码运行结果
使用RandomAccessFile类读取数据案例源码运行结果
总结
前言
File类可以提供对于文件本身的操作,而要对文件内容进行操作的话,可以用到RandomAccessFile类,此类属于随机读取类,可以随机读取一个文件中指定位置的数据。
主要方法
序号方法类型描述
1public RandomAccessFile(File file, String mode) throws FileNotFoundException构造接收File类的对象,指定操作路径,但是在设置时需要设置模式,r为只读,w为只写,rw为读写2public RandomAccessFile(String name, String mode) throws FileNotFoundException构造不再使用File类对象表示文件,而是直接输入了一个固定的文件路径3public void close() throws IOException普通关闭操作4public int read(byte[] b) throws IOException普通将内容读取到一个byte数组中5public final byte readByte() throws IOException普通读取一个字节6public final int readInt() throws IOException普通从文件中读取整型数据7public void seek(long pos) throws IOException普通设置读指针的位置8public final void writeBytes(String s) throws IOException普通将一个字符串写入到文件中,按字节的方式处理9public final void writeInt(int v) throws IOException普通将一个int型数据写入到文件,长度为4位10public 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");
if(file
.exists()){
file
.delete();
}
file
.createNewFile();
RandomAccessFile randomAccessFile
=
new RandomAccessFile(file
,"rw");
String name
= "zhangsan";
int age
= 30;
randomAccessFile
.writeBytes(name
);
randomAccessFile
.writeInt(age
);
name
= "lisi ";
age
= 20;
randomAccessFile
.writeBytes(name
);
randomAccessFile
.writeInt(age
);
name
= "wangwu ";
age
= 21;
randomAccessFile
.writeBytes(name
);
randomAccessFile
.writeInt(age
);
randomAccessFile
.close();
}
}
运行结果
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");
if(!file
.exists()){
System
.exit(0);
}
RandomAccessFile randomAccessFile
=
new RandomAccessFile(file
,"r");
byte[] name
= new byte[8];
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
.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();
}
}
运行结果
第二个学生的信息
---->姓名:lisi 年龄:
20
第一个学生的信息
---->姓名:zhangsan 年龄:
30
第三个学生的信息
---->姓名:wangwu 年龄:
21
总结
随机读写流可以实现对文件内容的操作,但是过于复杂,所以一般情况下操作文件会使用字节流或字符流,本类小伙伴们理解会使用即可哦,哈哈哈,充实的天天,吃饭!!!(小伙伴们有疑问欢迎评论留言哦)