Java IO--RandomAccessFile类
RandomAccessFile类的主要功能是完成随机读取功能,可以读取指定位置的内容。
因为在文件中,所有的内容都是按照字节存放的,都有固定的保存位置。
import java.io.File ;import java.io.RandomAccessFile ;public class RandomAccessFileDemo02{// 所有的异常直接抛出,程序中不再进行处理public static void main(String args[]) throws Exception{File f = new File("d:" + File.separator + "test.txt") ;// 指定要操作的文件RandomAccessFile rdf = null ;// 声明RandomAccessFile类的对象rdf = new RandomAccessFile(f,"r") ;// 以只读的方式打开文件String name = null ;int age = 0 ;byte b[] = new byte[8] ;// 开辟byte数组// 读取第二个人的信息,意味着要空出第一个人的信息rdf.skipBytes(12) ;// 跳过第一个人的信息for(int i=0;i<b.length;i++){b[i] = rdf.readByte() ;// 读取一个字节}name = new String(b) ;// 将读取出来的byte数组变为字符串age = rdf.readInt() ;// 读取数字System.out.println("第二个人的信息 --> 姓名:" + name + ";年龄:" + age) ;// 读取第一个人的信息rdf.seek(0) ;// 指针回到文件的开头for(int i=0;i<b.length;i++){b[i] = rdf.readByte() ;// 读取一个字节}name = new String(b) ;// 将读取出来的byte数组变为字符串age = rdf.readInt() ;// 读取数字System.out.println("第一个人的信息 --> 姓名:" + name + ";年龄:" + age) ;rdf.skipBytes(12) ;// 空出第二个人的信息for(int i=0;i<b.length;i++){b[i] = rdf.readByte() ;// 读取一个字节}name = new String(b) ;// 将读取出来的byte数组变为字符串age = rdf.readInt() ;// 读取数字System.out.println("第三个人的信息 --> 姓名:" + name + ";年龄:" + age) ;rdf.close() ;// 关闭}};