2016-09-14 4 views
0

私はゲームに取り組んでおり、私はjava.util.Propertiesを使って日付を保存しています。 XMLファイルにこの日付を保存したいのですが、いくつかの問題があります。java.util.Properties not working

これは、存在しない場合はOutputStream、XMLファイルおよびディレクトリを作成する方法です。

public String save(String date, String vname){ 
    //This is the method which saves the dates 

    //There are some controls 
    if(!dir.exists()){ 
     game.log.debug("Saves directory not exists. Creating...", -3); 
     if(!dir.mkdirs()) 
      game.log.debug("Impossible to create directory. The game can't save dates.", -1); 
     else game.log.debug("\"C:/MakeGame/saves\" successfully created.", 1); 
    } 

    if(ostream == null && save == null){ 
     try { 
      if(save == null){ 
       save = new File(dir.getPath() + game.getName() + " save.xml"); //THIS CREATES THE SAVE FILE IN XML FORMAT 
       ostream = new FileOutputStream(save); //THIS CREATE A NEW FILEOUTPUTSTREAM 
      } 
      if(!save.exists()){ 
       if(!save.createNewFile()) //NEW XML FILE CREATION 
        game.log.debug("Impossible to make new save file. The game can't save dates.", -1); 
       else game.log.debug("\"C:/MakeGame/saves/" + save.getName() + " \" successfully created.", 1); 
      } 
     } catch (FileNotFoundException ex) { 
      game.log.debug("File not found. The game can't save dates.", -1); 
     } catch (IOException ex) { 
      Logger.getLogger(Saver.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    String saved = ""; 

    if(save.exists()){ 
     saved = (String) saves.setProperty(date, vname); //SET THE PROPERTY 

     try { 
      saves.storeToXML(ostream, "Don't change this save files. This files are used by the games to load the progresses."); //STORES THE DATES IN XML FORMAT. 
     } catch (IOException ex) { 
      game.log.debug("Impossible to store this saves. The game can't save dates", -1); 
     } 
    } 

    return saved; 
} 

私は別の方法を使用して日付を読み込みます。

public String load(String vname) throws FileNotFoundException{ 
     if(save == null){ 
      save = new File(dir.getPath() + game + " save.save"); 
      istream = new FileInputStream(save); 
     } 

     if(save.exists()) try { 
      saves.load(istream); 
     } catch (IOException ex) { 
      game.log.debug("Error loading dates from save file. The game can't load dates.", -1); 
     } 
     if(!saves.isEmpty()){ 
      game.log.debug(saves.getProperty(vname) + " successfully loaded.", 0); 
      return saves.getProperty(vname); 
     } 
     return null; 
    } 

ただし、常にnullを返します。どうして?

答えて

0

save()メソッドでdate文字列をプロパティキーとして使用し、load()メソッドでvnameをキーとして使用しようとしています。

あなたの価値として、あなたの鍵とdateとして使用vnameあなたはこのようにプロパティを設定されています

saved = (String) saves.setProperty(vname, date); //SET THE PROPERTY 
+0

しかし、また、この方法は、(セーブ)が前回値として常にnullを返します。どうして? – Frankzt

+0

さて、私は解決しました。問題はあなたが言ったことです:D – Frankzt