File 如何在JavaFX中的文件中保存/加载值和设置?

File 如何在JavaFX中的文件中保存/加载值和设置?,file,properties,load,save,javafx-8,File,Properties,Load,Save,Javafx 8,如何保存值(来自文本字段、文本区域、单选按钮等)​​和文件中的设置(舞台位置、全屏或正常大小等)并从此文件加载?我有一个解决方案,但这个解决方案似乎不是最好的解决方案,因为它在很大程度上是“代码”(如果我使用许多组件) 我看了一下: 但这些例子非常简单: prop.setProperty("database", "localhost"); prop.setProperty("dbuser", "mkyong"); prop.setProperty("dbpassword", "passwor

如何保存值(来自文本字段、文本区域、单选按钮等)​​和文件中的设置(舞台位置、全屏或正常大小等)并从此文件加载?我有一个解决方案,但这个解决方案似乎不是最好的解决方案,因为它在很大程度上是“代码”(如果我使用许多组件)

我看了一下:

但这些例子非常简单:

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”)”对我来说并不是真正的“加载”。
没有显示我如何在文本框、单选按钮、复选框等组件中使用它

在我的代码片段中,我使用了不同的组件(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”));//是否加载所有组件的文本

我的解决方案“正确”吗? 这一切都按照我想要的方式进行。

但是有更好更快的解决方案吗?

除了使用属性和绑定API之外,您的解决方案似乎是正确的。如果您发现自己需要多次获取多个特定属性,请考虑创建一个静态的、可公开访问的成员,该成员代表加载的属性值,因此调用该值的方式与字段> StTeScript(PROSP.NAMEOVALIVE)一样容易。p>

您还可以考虑一个帮助您的属性文件独占访问的助手类,并负责加载和保存它。需要值的类可以使用键名查询帮助器类,这还允许您为基本类(int、long、double、boolean等)提供重载方法,这样您就不必重复代码来获取值并解析它,而可以使用

box.setSelected(Props.getBoolean(“box\u selected”)
,举个例子

helper类的好处是它删除了冗余的样板代码,还可以捕获解析异常并返回默认值,例如,如果找不到特定属性或其值无效,或者抛出异常并强制调用方处理它们