我在尝试反序列化对象列表时遇到一些问题。我已将对象列表序列化为 .dat 文件。如果我想稍后从该文件中检索数据,当我尝试反序列化该列表时,我不会得到所需的结果。这是代码。
序列化:
MyFile mf = new MyFile("2012-12-18.dat");
mf.Open();
FileOutputStream fos = mf.GetOS();
Iterator<Element> currencies = cube.select("Rate").iterator();
ISerializare[] lista = new ISerializare[31];
int i=0;
while (currencies.hasNext()){
MyCurrency newCurrency=new MyCurrency();
Element newElement=currencies.next();
newCurrency.setSymbol(newElement.attr("currency"));
newCurrency.setValue(Double.parseDouble(newElement.text()));
lista[i] = newCurrency;
System.out.println(newCurrency.toString());
i++;
}
DataOutputStream dos = new DataOutputStream(fos);
for(int j=0;j<i;j++){
lista[j].ObjectSerialization(dos);
}
dos.close();
public class MyFile {
File fisier;
String name;
public MyFile(String n){
name = n;
}
public void Open(){
fisier = new File(name);
}
public FileOutputStream GetOS() throws IOException{
return new FileOutputStream(fisier);
}
public FileInputStream GetIS() throws IOException{
return new FileInputStream(fisier);
}
}
MyFile mf = new MyFile("2012-12-18.dat");
mf.Open();
FileInputStream fis = mf.GetIS();
DataInputStream dis = new DataInputStream(fis);
for(ISerialization element:list){
element.ObjectDeserialization(dis);
System.out.println(element.toString());
这是 MyCurrency 类:
public class MyCurrency implements ISerialization
{
private String symbol;
private double value;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String toString(){
return symbol +" = "+value + " RON";
}
@Override
public void ObjectSerialization(DataOutputStream dos) throws IOException {
dos.writeDouble(value);
}
@Override
public void ObjectDeserialization(DataInputStream dis) throws IOException {
value = dis.readDouble();
}
你能告诉我出了什么问题吗?
最佳答案
Can you please tell me what is wrong?
有很多事情可能是错误的。既然你不具体,我就得发挥我的想象力了。
方法名称不遵循 Java 编码约定,这使得它们不易于阅读。使用代码格式化程序会有所帮助。
最明显的问题是你只写了 value
字段,表示 symbol
将是null
反序列化后。
还有
System.out.println(element.toString());
与
相同System.out.println(element);
还有
return symbol +" = "+value + " RON";
没有值的格式,因此它可能会打印 YEN 100.0 或 USD 100.0,而实际上应该是 YEN 100 和 USD 100.00,而且末尾有“RON”的原因并不明显。
<小时/>如果有帮助的话,我会这样写
Collection<Element> currencies = cube.select("Rate");
// write out
File mf = new File("2012-12-18.dat");
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(mf)));
dos.writeInt(currencies.size()); // so you know how many to read.
for (Element currency : currencies) {
MyMoney newCurrency = new MyMoney(currency);
newCurrency.writeTo(dos);
}
dos.close();
// read in
DataInputStream dis = new DataInputStream(
new BufferedInputStream(new FileInputStream(mf)));
int count = dis.readInt();
List<MyMoney> myMoneys = new ArrayList<>();
for (int i = 0; i < count; i++)
myMoneys.add(new MyMoney(dis));
dis.close();
public class MyMoney {
private final String symbol; // this is a currency
private final BigDecimal value;
public MyMoney(Element element) {
this(element.attr("currency"), new BigDecimal(element.text()));
}
public MyMoney(DataInputStream dis) throws IOException {
symbol = dis.readUTF();
value = new BigDecimal(dis.readUTF());
}
public MyMoney(String symbol, BigDecimal value) {
this.symbol = symbol;
this.value = value;
}
public String getSymbol() {
return symbol;
}
public BigDecimal getValue() {
return value;
}
public String toString() {
return symbol + " " + value;
}
public void writeTo(DataOutputStream dos) throws IOException {
dos.writeUTF(symbol);
dos.writeUTF(value.toString());
}
}
关于java - java 反序列化对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934266/