开发者社区> 问答> 正文

java I/O流的问题

源代码如下:

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)

能不能请大神告诉我这是为什么?

展开
收起
蛮大人123 2016-06-12 16:53:07 2338 0
1 条回答
写回答
取消 提交回答
  • 数据不是按照RECODE_SIZE为单位来存的,所以读的时候按RECODE_SIZE来seek offset的时候就错位了。

    2019-07-17 19:34:34
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载