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?

Recommended Answers

All 3 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.