2016-09-21 11 views
1

私のアプリはハッシュマップを停止する前に保存しています。ハッシュマップを開始すると再び同じハッシュマップが読み込まれ、変更が加えられます。私はシリアライゼーションを使用しています。シリアライズ後に複数のクラスで同じマップを使用していますか?

ストレージクラス:

public class Storage { 

private Map<String, String> storage; 
private String projectStorageFilePath; 

public Storage() { 
    this.storage = new ConcurrentHashMap<String, String>(); 
    makeDir(); 
} 

/** 
* If the file in which the map objects will be saved doesn't exist in the 
* user home directory it creates it. 
*/ 
private void makeDir() { 
    File projectHomeDir = new File(System.getProperty("user.home"), ".TestMap"); 
    String projectHomeDirPath = projectHomeDir.getAbsolutePath(); 
    File projectStorageFile = new File(projectHomeDirPath, "storage.save"); 
    projectStorageFilePath = projectStorageFile.getAbsolutePath(); 
    if (!projectHomeDir.exists()) { 
     projectHomeDir.mkdir(); 
     try { 
      projectStorageFile.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

@SuppressWarnings("unchecked") 
public boolean load() { 
    boolean isLoaded = false; 
    ObjectInputStream ois = null; 
    try { 

     File file = new File(projectStorageFilePath); 
     if (file.length() != 0) { 

      //loading the map 
      ois = new ObjectInputStream(new FileInputStream(file)); 
      storage = (ConcurrentHashMap<String, String>) ois.readObject(); 

      isLoaded = true; 

     } 
    } catch (IOException | ClassNotFoundException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (null != ois) { 
       ois.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return isLoaded; 
} 

public boolean save() { 
    boolean isSaved = false; 
    ObjectOutputStream oos = null; 
    try { 

     //saving 
     oos = new ObjectOutputStream(new FileOutputStream(projectStorageFilePath)); 
     oos.writeObject(storage); 

     isSaved = true; 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (null != oos) { 
       oos.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return isSaved; 
} 

public Map<String, String> getStorage() { 
    return this.storage; 
} 
} 

私はそのハッシュマップで何かをしようとしていているクラス:

public class DoSomethingWithMap { 

private Map<String, String> storage; 

public DoSomethingWithMap(Map<String, String> storage) { 
    this.storage = storage; 
} 

public void addToMap(String key, String value) { 
    this.storage.put(key, value); 
} 
public void printMap() { 
    System.out.println(this.storage); 
} 
} 

私はそれにそれが正常に動作し、初めてを実行します。

public class Main { 

public static void main(String[] args) { 
    Storage s = new Storage(); 
    DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage()); 

    if (s.load()) { 
     System.out.println(s.getStorage()); 
    } 

    something.addToMap("2", "test2"); 
    something.addToMap("4", "test4"); 
    something.addToMap("5", "test5"); 

    if (s.save()) { 
     System.out.println(s.getStorage()); 
    } 
} 
} 

出力:

私は再びメイン起動し、保存されたマップに変更を加えるしようとすると、

問題がある:

public static void main(String[] args) { 
    Storage s = new Storage(); 
    DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage()); 

    if (s.load()) { 
     System.out.println(s.getStorage()); 
    } 

    something.printMap(); 

    something.addToMap("6", "newTest"); 
    something.addToMap("7", "newTest"); 
    something.addToMap("8", "newTest"); 

    something.printMap(); 

    if (s.save()) { 
     System.out.println(s.getStorage()); 
    } 
} 

出力:それは明らかDoSomethingWithMapクラスだったマップを使用していないされて

{3=test3, 4=test4, 5=test5}   //loading the map works fine 
{}         //here it should be same as previous line but is not 
{6=newTest, 7=newTest, 8=newTest} //DoSomethingWithMap.printMap is printing only the changes during runtime 
{3=test3, 4=test4, 5=test5}  // changes during runtime are not saved 

それに与えられた。どうして?どの地図が使用されていますか?どのように私はそれを修正することができます?

ありがとうございます。

+0

doSomethingメソッドでHashMap Staticを試してみてください。 –

+0

私は試しましたが、動作しません。 – Yuliyan

答えて

0

あなたのloadメソッドで地図の新しいインスタンスを作成している:

storage = (ConcurrentHashMap<String, String>) ois.readObject(); 

あなたは、現在のマップをクリアしてからロードされた1からすべての値を追加することができます修正するには:

//loading the map 
ois = new ObjectInputStream(new FileInputStream(file)); 
storage.clear(); 
storage.putAll((ConcurrentHashMap<String, String>) ois.readObject()); 

このようなエラーを未然に防止するには、これらのフィールドを最終的にすることができます。エラーレポートが表示されます。

+0

私はストレージの新しいインスタンスを作成しているのを知らなかった。それをクリアしていただきありがとうございます。 – Yuliyan

関連する問題