尝试反序列化对象列表时遇到一些问题。我已将对象列表分类为.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();
}
你能告诉我有什么问题吗?
答案 0 :(得分:1)
你能告诉我有什么问题吗?
有很多事情可能是错的。既然你不具体,我将不得不用我的想象力。
方法名称不遵循Java编码约定,因为它们不易于阅读。使用代码格式化器会有所帮助。
最明显的问题是,您只需编写value
字段,这意味着在您反序列化后symbol
将为null
。
另外
System.out.println(element.toString());
与
相同System.out.println(element);
和
return symbol +" = "+value + " RON";
没有格式化值,所以当它应该是100日元和100美元时它可能会打印YEN 100.0或100.0美元,并且为什么你最后有“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());
}
}