java - java 反序列化对象列表

标签 java serialization deserialization

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

相关文章:

java - 如何在 Java 中拆分具有多个分隔符的字符串

scala - Spark 内部类 Kryo 注册

c# - 使用 JsonConvert.SerializeObject 序列化派生列表

java - 使用 Jackson 反序列化树形结构 JSON 中的值

java - 使用 Jackson 根据另一个字段(键)映射 JSON 字段(到值)

java - 无需编码将字符串转换为字节数组?

java - 我有这个错误 : java. sql.SQLException: Java 应用程序中的列名无效

java - 将数据从我的表转换为 Mahout 数据模型

xml - 防止 IEnumerable 和 ICollection<T> & 继承类型的 XML 序列化

C# 反序列化 - 捕获 TargetInitationException 是否安全?