RandomAccessFile无法读取对象后面的数据显示EOFException异常问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

RandomAccessFile无法读取对象后面的数据显示EOFException异常问题

2016-03-03 11:41:02 2822 1

代码:


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

public class RandomFileTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];

staff[0] = new Employee( "Carl Cracker ", 75000, 1987, 12, 15);
staff[1] = new Employee( "Harry Hacker ", 50000, 1989, 10, 1);
staff[2] = new Employee( "Tony Tester ", 40000, 1990, 3, 15);

try
{
// 对象写入数据文件里面
DataOutputStream out = new DataOutputStream(new FileOutputStream( "C:\\employee.dat "));
for (Employee e : staff)
e.writeData(out);//自定义的写入文件的方法
out.close();

// 随机访问刚才写入的文件
RandomAccessFile in = new RandomAccessFile( "C:\\employee.dat ", "r");
// 计算文件里存放了几个对象
int n = (int)(in.length() / Employee.RECORD_SIZE);
Employee[] newStaff = new Employee[n];

// 从后往前读取数据,类似于C嘎嘎的指针
for (int i = n - 1; i >= 0; i--)
{
newStaff[i] = new Employee();
in.seek(i * Employee.RECORD_SIZE);
newStaff[i].readData(in);
}
in.close();

// 输出这些东西
for (Employee e : newStaff)
System.out.println(e);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

class Employee
{
public Employee() {}

public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}

public String getName()
{
return name;
}

public double getSalary()
{
return salary;
}

public Date getHireDay()
{
return hireDay;
}

/**
 Writes employee data to a data output
 */
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

public String toString()
{
return getClass().getName()
+ "[name= " + name
+ ",salary= " + salary
+ ",hireDay= " + hireDay
+ "] ";
}

/**
 Writes employee data to a data output
 @param out the data output
 */
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));
}

/**
 Reads employee data from a data input
 @param in the data input
 */
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;
public static final int RECORD_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);
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();
}
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:771)
at java.io.RandomAccessFile.readLong(RandomAccessFile.java:804)
at java.io.RandomAccessFile.readDouble(RandomAccessFile.java:850)
at Employee.readData(RandomFileTest.java:120)
at RandomFileTest.main(RandomFileTest.java:38)
好像是读到文件结尾了,我试着添加一个空的对象就能正常运行了
貌似是Employee的readData方法读取double时后面是空的,但是我在UE里看没到文件结尾啊?
77
而且 name = DataIO.readFixedString(NAME_SIZE, in);
输出Tony Tester肯定没问题,就是绿色的部分被当成EOF无法被继续读取,这是怎么一回事啊?

取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 18:51:09

    `
    class DataIO
    {
    public static String readFixedString()
    ...
    in.skipBytes(2*(size-1));`
    改为:

    in.skipBytes(2*(size-i));

    0 0
相关问答

1

回答

如何解决RetractStream流写mysql出现java.sql.SQLException?

2021-12-03 18:14:25 423浏览量 回答数 1

1

回答

Exception in thread "main" org.hibernate.SessionEx

2021-01-25 15:38:18 1530浏览量 回答数 1

1

回答

求助:启动消息队列RocketMQ版的客户端时提示“UnknownHostException”

2020-12-07 15:23:19 440浏览量 回答数 1

1

回答

org.hibernate.LazyInitializationExceptio?报错

2020-06-22 18:13:47 227浏览量 回答数 1

1

回答

Could not obtain transaction-synchronize?400报错

2020-06-07 17:21:13 274浏览量 回答数 1

1

回答

将Exception传递给自定义的Exception Handler方法

2020-03-21 21:11:24 953浏览量 回答数 1

1

回答

企业邮箱在iphone8上出现证书不可信

2019-03-19 13:09:01 2198浏览量 回答数 1

0

回答

ossimport2.3.1 Thread.sleep InterruptedException

2018-09-14 11:06:35 781浏览量 回答数 0

0

回答

javax.faces.context.ExceptionHandlerFactory

2016-05-31 09:42:22 3010浏览量 回答数 0

0

回答

Lock wait timeout exceededtry restarting transaction

2015-12-14 14:37:45 4271浏览量 回答数 0
+关注
蛮大人123
我说我不帅他们就打我,还说我虚伪
0
文章
7733
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载