2014-10-03 218 views
1

如何將文件中的值(來自textfields,textareas,radiobuttons等)和設置(舞臺位置,全屏或正常大小等)保存在文件中並從該文件中加載?我有一個解決方案,但這個解決方案似乎不是最好的解決方案,因爲它非常「代碼」(如果我使用了很多組件)。如何在JavaFX中的文件/文件中保存/加載值和設置?

我看看這個: Reading Properties file in Java

http://www.mkyong.com/java/java-properties-file-examples/

但這些例子非常簡單:

prop.setProperty("database", "localhost"); 
prop.setProperty("dbuser", "mkyong"); 
prop.setProperty("dbpassword", "password"); 

System.out.println(prop.getProperty("database")); 
System.out.println(prop.getProperty("dbuser")); 
System.out.println(prop.getProperty("dbpassword")); 

但 'System.out.println (prop.getProperty("value"));' 是不是真的 '裝載' 對我來說。 沒有顯示如何在組件中使用它,例如textfiels,單選按鈕,複選框等。

在我的代碼片段中,我使用了不同的組件(textfield,datepicker,rb,cb等)。 這裏是我的代碼片段:

節能:

public void speichern (ActionEvent event) throws IOException 
    { 
     if (filename == null) 
      speichernUnter (event); 
     else 
     { 
      Properties prop = new Properties (); 
      FileOutputStream fos = null; 
      try 
      { 

      prop.setProperty ("Name", textField.getText ()); // Save a textfield 
      if (radioButton.isSelected ()) 
       prop.setProperty ("RadioButtonState", "yes"); // Save rb state 
      if (checkBox.isSelected ()) 
      { 
       prop.setProperty ("CheckBoxState", "yes"); // Save cb state 
      } else 
       prop.setProperty ("CheckBoxState", "no"); // or not 

      prop.setProperty ("Date", datePicker.getValue ().toString ()); //Save date 
      prop.setProperty ("Text", textArea.getText ()); // Save long text 


      fos = new FileOutputStream (filename); 


      // Save settings in file 
      prop.storeToXML (new FileOutputStream (filename), "Values and settings", "UTF-8"); 
     } catch (IOException ex) 
     { 
      ex.printStackTrace (); 
     } finally 
     { 
      if (fos != null) 
       fos.close (); 
     } } 
    } 
    public void speichernUnter (ActionEvent event) 
    { 
     init (); 
     FileChooser fileChooser = new FileChooser (); 

     //Set extension filter 
     FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter ("PROPERTIES Dateien (*.properties)", "*.properties"); 
     fileChooser.getExtensionFilters ().add (extFilter); 

     //Show save file dialog 
     File file = fileChooser.showSaveDialog (stage); 
     if (file != null) 
     { 
      try 
      { 
       FileWriter fileWriter = new FileWriter (file); 
       fileWriter.close (); 
       filename = file.toString (); 
       speichern (event); 
      } catch (IOException ex) 
      { 
       Logger.getLogger (Kontroller.class.getName ()).log (Level.SEVERE, null, ex); 
      } 
     } 
    } 

加載:

public void laden (ActionEvent event) throws IOException 
    { 
     if (filename == null) 
      ladenVon (event); 
     else 
     { 
      Properties prop = new Properties (); 
      FileInputStream fis = null; 
      try 
      { 

       fis = new FileInputStream (filename); 

       // Load Properties from saved XML file 
       prop.loadFromXML (fis); 

       textField.setText (prop.getProperty ("Name")); // load the text 

       if (prop.getProperty ("RadioButtonState").equals ("yes")) // load rb state 
       { 
        radioButton.setSelected (); 
       } 

        if (checkBox.isSelected ()) // Load cb state 
         checkBox.setSelected (false); 
        checkBox.setSelected (true); 
       } 

       datePicker.setValue (LocalDate.parse (prop.getProperty ("Date"))); // load date 

       textArea.setText (prop.getProperty ("Text")); // Load long text 
      } catch (IOException ex) 
      { 
       ex.printStackTrace (); 
      } finally 
      { 
       if (fis != null) 
        fis.close (); 
      } 
     } 
    } 
    public void ladenVon (ActionEvent event) throws IOException 
    { 
     init (); 
     FileChooser fileChooser = new FileChooser (); 


     FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter ("PROPERTIES Dateien (*.properties)", "*.properties"); 
     fileChooser.getExtensionFilters ().add (extFilter); 

     //Show load file dialog 
     File file = fileChooser.showOpenDialog (stage); 
     if (file != null) 
     { 
      try 
      { 
       FileReader fileReader = new FileReader (file); 
       fileReader.close (); 
       filename = file.toString (); 
       laden (event); 
      } catch (IOException ex) 
      { 
       Logger.getLogger (Kontroller.class.getName ()).log (Level.SEVERE, null, ex); 
      } 
     } 
    } 

難道我真的要保存做這個 'prop.setProperty ("Name", textField.getText ());',這 'textField.setText (prop.getProperty ("Name")); // load the text' 加載所有組件?

我的解決方案是否正確?這一切都按我想要的方式工作。 我的問題可能很愚蠢。 但是有更好更快的解決方案嗎?

在此先感謝

回答

4

缺少使用屬性和綁定API,您的解決方案似乎是正確的。如果您發現自己需要多次獲取多個特定屬性,請考慮製作一個靜態且可公開訪問的成員,以表示加載的屬性值,因此調用該值以獲取該值的操作就像field.setText(Props.NAME_VALUE)一樣簡單。

你也可以考慮一個輔助類,它擁有對你的屬性文件的獨佔訪問權,並負責加載和保存它。需要值的類可以使用鍵名查詢您的助手類,這也允許您爲原始類(int,long,double,boolean等)提供重載方法,因此您不必重複代碼即可獲取值並解析它,並可以使用box.setSelected(Props.getBoolean("box_selected"))作爲示例。

幫助程序類的好處是它可以刪除多餘的樣板代碼,並且還可以捕獲解析異常並返回默認值,例如,如果找不到特定屬性或其值無效,或者拋出異常並強迫呼叫者處理它們。

相關問題