title: How can I save / load values and settings in a file / from a file in JavaFX?
body: <p>How can I save values (from textfields, textareas, radiobuttons, etc.) ​​and settings (position of the stage, fullscreen or normal size, etc.) in a file and load from this file ? I have a solution, but this solution does not seem to be the best solution, because it is very much "code" (if I use many components). </p>
  
  <p>I had a look at this: 
  <a href="https://stackoverflow.com/questions/8285595/reading-properties-file-in-java?rq=1">Reading Properties file in Java</a> </p>
  
  <p><a href="http://www.mkyong.com/java/java-properties-file-examples/" rel="nofollow noreferrer">http://www.mkyong.com/java/java-properties-file-examples/</a> </p>
  
  <p>But these examples are very simple:  </p>
  
  <pre><code>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"));
  </code></pre>
  
  <p>But '<code>System.out.println (prop.getProperty("value"));</code>' is not really 'loading' for me.
  There is not shown how I use it in components like textfiels, radiobuttons, checkboxes and so on. </p>
  
  <p>In my code snippet I use different components (textfield, datepicker, rb, cb etc.). 
  Here's my code snippet: </p>
  
  <p>Saving: </p>
  
  <pre><code>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); 
              } 
          } 
      }
  </code></pre>
  
  <p>Loading: </p>
  
  <pre><code>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); 
              } 
          } 
      }
  </code></pre>
  
  <p>Do I really have to do this '<code>prop.setProperty ("Name", textField.getText ( ) );</code>' for saving and this '<code>textField.setText (prop.getProperty ("Name") ); // load the text</code> ' for loading for all components? </p>
  
  <p>Is my solution 'correct'?
  It all works the way I want it.<br>
  But is there a better and faster solution?</p>