What is the simplest way to do settings files in Java?

前端 未结 8 1544
清歌不尽
清歌不尽 2021-01-03 05:47

Obviously enough, I want to avoid hardcoding paths and such into my application, and as a result, I\'d like to make a settings file that will store simple things like string

8条回答
  •  误落风尘
    2021-01-03 06:21

    Based on the fact you said simplest:

    Please keep in mind I didn't do any exception handling. You will need to do that for the Streams.

        // Save Settings
        Properties saveProps = new Properties();
        saveProps.setProperty("path1", "/somethingpath1");
        saveProps.setProperty("path2", "/somethingpath2");
        saveProps.storeToXML(new FileOutputStream("settings.xml"), "");
    
        // Load Settings
        Properties loadProps = new Properties();
        loadProps.loadFromXML(new FileInputStream("settings.xml"));
        String path1 = loadProps.getProperty("path1");
        String path2 = loadProps.getProperty("path2");
    

提交回复
热议问题