源代码如下:
public class RandomFileTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("王海", 75000, 1987, 12, 15);
staff[1] = new Employee("王明", 50000, 1989, 10, 1);
staff[2] = new Employee("周晓", 40000, 1990, 3, 15);
try {
DataOutputStream out = // 字节文件流。实现了DataOutput接口。以二进制文件输出流为参数
new DataOutputStream(new FileOutputStream("employee.dat"));
for (Employee e : staff)
e.writeData(out);
out.close();
// 以二级制文件为参数,建立RandomAccessFile对象。“r”为“只读”
RandomAccessFile in = new RandomAccessFile("employee.dat", "r");
int n = (int) (in.length() / Employee.RECODE_SIZE);
Employee[] newStaff = new Employee[n];
for (int i = n - 1; i >= 0; i--) {
newStaff[i] = new Employee();
in.seek(i * Employee.RECODE_SIZE);
newStaff[i].readData(in);
}
in.close();
for (Employee e : newStaff) {
System.out.println(e);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Employee {
public Employee() {
}
public Employee(String n, double s, int year, int mouth, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, mouth - 1, day);
hireDay = calendar.getTime();
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
public String toString() {
return getClass().getName() + "[name+" + name + ",salary" + salary
+ ",hireDay=" + hireDay + "]";
}
public void writeData(DataOutput out) throws IOException {
DataIO.writeFixedString(name, NAME_SIZE, out);
out.writeDouble(salary);
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(hireDay);
out.writeInt(calendar.get(calendar.YEAR));
out.writeInt(calendar.get(calendar.MONTH + 1));
out.writeInt(calendar.get(calendar.DAY_OF_MONTH));
}
public void readData(DataInput in) throws IOException {
name = DataIO.readFixedString(NAME_SIZE, in);
salary = in.readDouble();
int y = in.readInt();
int m = in.readInt();
int d = in.readInt();
GregorianCalendar calendar = new GregorianCalendar(y, m - 1, d);
hireDay = calendar.getTime();
}
public static final int NAME_SIZE = 40;// 姓名按照40個字符設計
public static final int RECODE_SIZE = 2 * NAME_SIZE + 8 + 4 + 4 + 4;
private String name;
private double salary;
private Date hireDay;
}
class DataIO {
public static String readFixedString(int size, DataInput in)
throws IOException {
StringBuffer b = new StringBuffer(size);// 较StringBuffer效率更高,但是不支持多线程
int i = 0;
boolean more = true;
while (more && i < size) {
char ch = in.readChar();//将二进制数据转换成字符读出
i++;
if (ch == 0) {
more = false;
} else {
b.append(ch);
}
}
in.skipBytes(2 * (size - 1));//跳过无效字符
return b.toString();//将StringBulider对象中的字符串显示出来
}
public static void writeFixedString(String s, int size, DataOutput out)throws IOException {
for (int i = 0; i < size; i++) {
char ch = 0;
if (i < s.length()) {
ch = s.charAt(i);
}
out.writeChar(ch);
}
}
}
控制台现实信息如下:
java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:725)
at com.Sixping.Ncre07.Employee.readData(RandomFileTest.java:85)
at com.Sixping.Ncre07.RandomFileTest.main(RandomFileTest.java:25)
能不能请大神告诉我这是为什么?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。