0

尝试反序列化对象列表时遇到一些问题。我已将对象列表序列化为 .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());

这是 MyCurency 类:

    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();
}

你能告诉我有什么问题吗?

4

1 回答 1

1

你能告诉我有什么问题吗?

有很多事情可能是错误的。由于您不具体,我将不得不发挥我的想象力。

方法名称不遵循 Java 编码约定,这使它们不易阅读。使用代码格式化程序会有所帮助。

最明显的问题是您只编写value字段,这意味着您symbolnull在反序列化它之后。

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());
    }
}
于 2012-12-18T13:43:55.497 回答