I have configuration properties implemented as a java.util.Properties Object with the mappings saved in a config.properties file. I have managed to update the properties in a user input panel, so the user can change any settings while the application is live.

However I want any changes a user makes to be saved and be automatically set the next time the application is opened. Any ideas on how I can do this?

Every time the user changes something, overwrite the config.properties file, and just always try to load the file when your app starts? There might be more to it I don't know about.

I have configuration properties implemented as a java.util.Properties Object with the mappings saved in a config.properties file. I have managed to update the properties in a user input panel, so the user can change any settings while the application is live.

However I want any changes a user makes to be saved and be automatically set the next time the application is opened. Any ideas on how I can do this?

This might help you.

http://download.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html

Thanks for replies, but I still cannot get this to work. I have a settings panel where the user can change the settings and the apply and cancel buttons are controled by this ActionListener

@Override
    public void actionPerformed(ActionEvent e) {

		OutputStream out;
        //Handle open button action.
        if (e.getSource() == applyButton) {

        	properties.setProperty("PropertyOne", String.valueOf(propertyOneInput.getText()));
        	properties.setProperty("PropertyTwo", String.valueOf(propertyTwoCheckBox.isSelected()));
			
        	if(restoreFactoryDefaultsCheckBox.isSelected()){
        		
        		InputStream in;
				try {
					properties.clear();
//					properties.load(Settings.class.getResourceAsStream("defaultConfig.properties"));
					in = Settings.class.getResourceAsStream("defaultConfig.properties");
					properties.load(in);
					in.close();
					
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
        	}
        	
        	try {// This should save the proerties until the program is next run, but its not working
				out = new FileOutputStream("config.properties");
				properties.store(out, "---No Comment---");
				
				out.close();
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
        	
        	frame.dispose();
        	
        }else if(e.getSource() == cancelButton){
        	
        	frame.dispose();
        }
    }

Would greatly appreciate any further help

This article has been dead for over six months. Start a new discussion instead.