import java.io.*;
public class RandomAccessTest {

public static void main(String[] args) throws Exception  {
// TODO 自动生成方法存根
Employee e1=new Employee("zhan",23);
Employee e2=new Employee("lisi",23);
Employee e3=new Employee("wangwu",23);
RandomAccessFile ra=
new RandomAccessFile("c:\\Myjava\\1.txt","rw");
ra.write(e1.name.getBytes());
ra.writeInt(e1.age);
ra.write(e2.name.getBytes());
ra.writeInt(e2.age);
ra.write(e3.name.getBytes());
ra.writeInt(e3.age);
ra.close();
RandomAccessFile raf=new RandomAccessFile("c:\\Myjava\\1.txt","r");
int LEN=8;
raf.skipBytes(12);
System.out.println("第二个员工信息:");
String str="";
for(int i=0;i<8;i++)
str=str+(char)raf.readByte();
System.out.println("name:"+str);
System.out.println("age:"+raf.readInt());

System.out.println("第一个员工信息:");
raf.seek(0);
str="";
for(int i=0;i<8;i++)
str=str+(char)raf.readByte();
System.out.println("name:"+str);
System.out.println("age:"+raf.readInt());
System.out.println("第三个员工信息:");
raf.skipBytes(12);
str="";
    for(int i=0;i<LEN;i++)
str=str+(char)raf.readByte();
System.out.println("name:"+str.trim());
System.out.println("age:"+raf.readInt());
   
raf.close();  
}}
class Employee
{
String   name;
int     age;
final static  int LEN=8;
public Employee(String name,int  age)
{
this.name=name;
this.age=age;
if(name.length()>LEN)
{
name=name.substring(0,8);
}
else
{
while(name.length()<LEN)
name=name+"\u0000";
}
}
}