SixPing 2016-04-16 17:04 采纳率: 42.9%
浏览 1470

java I/O流的问题,请大神指点

源代码如下:
package com.Sixping.Ncre07;

import java.io.*;
import java.util.*;

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)

能不能请大神告诉我这是为什么?
请大神介绍介绍经验。

  • 写回答

2条回答

  • devmiao 2016-04-16 19:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示