本帖最后由 吴亨 于 2011-12-17 12:57 编辑
import java.io.*;
public class TestFile {
public static void main(String[] args) throws Exception
{
Employee s1 = new Employee("zhangsan",21);
Employee s2 = new Employee("lisi",23);
Employee s3 = new Employee("wangwu",24);
RandomAccessFile f = new RandomAccessFile("abc.txt","rw");
f.write(s1.name.getBytes());
f.writeInt(s1.age);
f.write(s2.name.getBytes());
f.writeInt(s2.age);
f.write(s3.name.getBytes());
f.writeInt(s3.age);
f.close();
final int LEN = 8;
byte[] Buffer= new byte[8];
f.skipBytes(LEN+4);
f.read(Buffer);
System.out.println(new String(Buffer)+","+f.read());
f.seek(0);
Buffer = null;
f.read(Buffer);
System.out.println(new String(Buffer)+","+f.read());
f.skipBytes(LEN+4);
Buffer = null;
f.read(Buffer);
System.out.println(new String(Buffer)+","+f.read());
f.close();
}
}
class Employee {
public String name = "unknown";
public int age = 0;
public static final int LEN = 8;
public Employee(String name,int age) {
if(name.length()<LEN)
name += "\u0000";
else
name = name.substring(0,LEN);
this.name = name;
this.age = age;
}
}
编译没问题,但执行却有问题。 |
|