import java.io.*;
class RandomAccessFileDemo
{
public static void main(String[] args) throws IOException
{
write();
read();
}
public static void write()throws IOException
{
RandomAccessFile acf=new RandomAccessFile("随机.txt","rw");
acf.write("李四".getBytes());
acf.writeInt(89);
acf.write("王五".getBytes());
acf.writeInt(45);
acf.skipBytes(8*4);
acf.write("李双".getBytes());
acf.writeInt(80);
acf.close();
}
public static void read()throws IOException
{
RandomAccessFile acf=new RandomAccessFile("随机.txt","rw");
acf.seek(8*3);// 要求在写入时候按照姓名和年龄共占八位的情况写
byte[] arr=new byte[4];
acf.read(arr);
System.out.println(new String(arr)+acf.readInt());
}
}
|