2010-09-01 89 views
2

由於某些原因,當我反序列化我的引號ArrayList時,我沒有得到正確的對象。我想確保每當我讀/寫我的對象時,它將始終是同一個對象。我的序列化/反序列化是否正確?

序列化代碼:

private void serializeQuotes(){ 
     FileOutputStream fos; 
     try { 
      fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE); 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 
      oos.writeObject(quotes); 
      oos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    @SuppressWarnings("unchecked") 
    private void deserializeQuotes(){ 
     try{ 
      FileInputStream fis = openFileInput(Constants.FILENAME); 
      ObjectInputStream ois = new ObjectInputStream(fis); 
      quotes = (ArrayList<Quote>) ois.readObject(); 
      fis.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     }catch(ClassNotFoundException e){ 
      e.printStackTrace(); 
     } 
    } 

這裏是你爲什麼不寫對象之前刪除文件 「serializeQuotes()」 我的報價對象

package org.stocktwits.model; 

import java.io.Serializable; 

public class Quote implements Serializable { 

    private static final long serialVersionUID = 1L; 

    public String symbol; 
    public String name; 
    public String change; 
    public String percentChange; 
    public String open; 
    public String daysHigh; 
    public String daysLow; 
    public String volume; 
    public String peRatio; 
    public String marketCapitalization; 
    public String yearHigh; 
    public String yearLow; 
    public String lastTradePriceOnly; 

    public String getSymbol() { 
     return symbol; 
    } 
    public void setSymbol(String symbol) { 
     this.symbol = symbol; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getChange() { 
     return change; 
    } 
    public void setChange(String change) { 
     this.change = change; 
    } 
    public String getPercentChange() { 
     return percentChange; 
    } 
    public void setPercentChange(String percentChange) { 
     this.percentChange = percentChange; 
    } 
    public String getOpen() { 
     return open; 
    } 
    public void setOpen(String open) { 
     this.open = open; 
    } 
    public String getDaysHigh() { 
     return daysHigh; 
    } 
    public void setDaysHigh(String daysHigh) { 
     this.daysHigh = daysHigh; 
    } 
    public String getDaysLow() { 
     return daysLow; 
    } 
    public void setDaysLow(String daysLow) { 
     this.daysLow = daysLow; 
    } 
    public String getVolume() { 
     return volume; 
    } 
    public void setVolume(String volume) { 
     this.volume = volume; 
    } 
    public String getPeRatio() { 
     return peRatio; 
    } 
    public void setPeRatio(String peRatio) { 
     this.peRatio = peRatio; 
    } 
    public String getMarketCapitalization() { 
     return marketCapitalization; 
    } 
    public void setMarketCapitilization(String marketCapitalization) { 
     this.marketCapitalization = marketCapitalization; 
    } 
    public String getYearHigh() { 
     return yearHigh; 
    } 
    public void setYearHigh(String yearHigh) { 
     this.yearHigh = yearHigh; 
    } 
    public String getYearLow() { 
     return yearLow; 
    } 
    public void setYearLow(String yearLow) { 
     this.yearLow = yearLow; 
    } 

    public String getLastTradePriceOnly() { 
     return lastTradePriceOnly; 
    } 

    public void setLastTradePriceOnly(String lastTradePriceOnly) { 
     this.lastTradePriceOnly = lastTradePriceOnly; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((change == null) ? 0 : change.hashCode()); 
     result = prime * result 
       + ((daysHigh == null) ? 0 : daysHigh.hashCode()); 
     result = prime * result + ((daysLow == null) ? 0 : daysLow.hashCode()); 
     result = prime 
       * result 
       + ((lastTradePriceOnly == null) ? 0 : lastTradePriceOnly 
         .hashCode()); 
     result = prime 
       * result 
       + ((marketCapitalization == null) ? 0 : marketCapitalization 
         .hashCode()); 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     result = prime * result + ((open == null) ? 0 : open.hashCode()); 
     result = prime * result + ((peRatio == null) ? 0 : peRatio.hashCode()); 
     result = prime * result 
       + ((percentChange == null) ? 0 : percentChange.hashCode()); 
     result = prime * result + ((symbol == null) ? 0 : symbol.hashCode()); 
     result = prime * result + ((volume == null) ? 0 : volume.hashCode()); 
     result = prime * result 
       + ((yearHigh == null) ? 0 : yearHigh.hashCode()); 
     result = prime * result + ((yearLow == null) ? 0 : yearLow.hashCode()); 
     return result; 
    } 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Quote other = (Quote) obj; 
     if (change == null) { 
      if (other.change != null) 
       return false; 
     } else if (!change.equals(other.change)) 
      return false; 
     if (daysHigh == null) { 
      if (other.daysHigh != null) 
       return false; 
     } else if (!daysHigh.equals(other.daysHigh)) 
      return false; 
     if (daysLow == null) { 
      if (other.daysLow != null) 
       return false; 
     } else if (!daysLow.equals(other.daysLow)) 
      return false; 
     if (lastTradePriceOnly == null) { 
      if (other.lastTradePriceOnly != null) 
       return false; 
     } else if (!lastTradePriceOnly.equals(other.lastTradePriceOnly)) 
      return false; 
     if (marketCapitalization == null) { 
      if (other.marketCapitalization != null) 
       return false; 
     } else if (!marketCapitalization.equals(other.marketCapitalization)) 
      return false; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     if (open == null) { 
      if (other.open != null) 
       return false; 
     } else if (!open.equals(other.open)) 
      return false; 
     if (peRatio == null) { 
      if (other.peRatio != null) 
       return false; 
     } else if (!peRatio.equals(other.peRatio)) 
      return false; 
     if (percentChange == null) { 
      if (other.percentChange != null) 
       return false; 
     } else if (!percentChange.equals(other.percentChange)) 
      return false; 
     if (symbol == null) { 
      if (other.symbol != null) 
       return false; 
     } else if (!symbol.equals(other.symbol)) 
      return false; 
     if (volume == null) { 
      if (other.volume != null) 
       return false; 
     } else if (!volume.equals(other.volume)) 
      return false; 
     if (yearHigh == null) { 
      if (other.yearHigh != null) 
       return false; 
     } else if (!yearHigh.equals(other.yearHigh)) 
      return false; 
     if (yearLow == null) { 
      if (other.yearLow != null) 
       return false; 
     } else if (!yearLow.equals(other.yearLow)) 
      return false; 
     return true; 
    } 
} 
+0

關閉您的輸入流 – Bozho 2010-09-01 19:32:54

+0

輸入流關閉。接得好。問題仍然存在 – 2010-09-01 19:43:00

+0

我寫/序列化時看不到'引號'的數據類型。它應該是嚴格的ArrayList bragboy 2010-09-01 19:50:43

回答

1

。那樣你就可以肯定,那裏只有一個物體。

private void serializeQuotes(){ 
     FileOutputStream fos; 
     File file = new File(Constants.FILENAME); 
     if (file.exists()) file.delete(); 
     try { 
      fos = openFileOutput(file, Context.MODE_PRIVATE); 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 
      oos.writeObject(quotes); 
      oos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

或者,如果您不想每次都刪除文件,請在從中讀取對象時使用某種迭代。