我制作了一个可外部化的对象 - 用户。它在写入和从存储中读取时工作正常,除非我退出应用程序并重新打开它。然后发生此错误:
java.io.EOFException at java.io.DataInputStream.readFully(DataInputStream.java:197)at java.io.DataInputStream.readUTF(DataInputStream.java:609)at java.io.DataInputStream.readUTF(DataInputStream.java:564)at com.codename1.io.Util.readUTF(Util.java:962)
我在stateMachine中注册了以下类:
@Override
protected void initVars(Resources res) {
Util.register("User", User.class);
}
这是班级:
package userclasses;
import com.codename1.io.Externalizable;
import com.codename1.io.Util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
/**
*
*
*/
public class User implements Externalizable {
private static final int VERSION = 1;
public int userID;
public String username;
public String password;
public String firstname;
public String surname;
public String email;
public Date registerDate;
public boolean paidUser;
public double runStep;
public double walkStep;
public User(){
}
@Override
public int getVersion() {
return VERSION;
}
@Override
public void externalize(DataOutputStream out) throws IOException {
out.write(userID);
Util.writeUTF(username, out);
Util.writeUTF(password, out);
Util.writeUTF(firstname, out);
Util.writeUTF(surname, out);
Util.writeUTF(email, out);
out.writeLong(registerDate.getTime());
out.writeBoolean(paidUser);
out.writeDouble(runStep);
out.writeDouble(walkStep);
}
@Override
public void internalize(int version, DataInputStream in) throws IOException {
userID = in.readInt();
username = Util.readUTF(in);
password = Util.readUTF(in);
firstname = Util.readUTF(in);
surname = Util.readUTF(in);
email = Util.readUTF(in);
registerDate = new Date(in.readLong());
paidUser = in.readBoolean();
runStep = in.readDouble();
walkStep = in.readDouble();
}
@Override
public String getObjectId() {
return "User";
}
}
答案 0 :(得分:0)
替换:
out.write(userID);
使用:
out.writeInt(userID);
默认的写入方法相当于writeByte
。