Developer Tip: Properties

As a developer, you may need to work with properties time to time. Regardless of your projects requirements, there are some good practices that mostly is common between projects, I will share some of my habits here.

Using “default” properties for our application execution. below is a code snippet you can use.

      Properties defaultProp = null;
    // then load the defualt properties into our applicaiton.
    Properties applicationCustomized = null;

    public PracticeDefaultProperties() {
        defaultProp = new Properties();
        applicationCustomized = new Properties(defaultProp);
    }

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AuthProvider;
import java.util.Properties;

/**
*
* @author Navid_
*/
public class PracticeDefaultProperties {

// this is a place to load stuff from application package myFile.properties into this as default stuff.
Properties defaultProp = null;
// then load the defualt properties into our applicaiton.
Properties applicationCustomized = null;

public PracticeDefaultProperties() {
defaultProp = new Properties();
applicationCustomized = new Properties(defaultProp);
}

/**
* reading and loading from default properties file.
*
* @throws IOException
*/
public void propertiesLoader() throws IOException {

try (BufferedReader reader = Files.newBufferedReader(Paths.get("./resources/myFile.properties"))) {
defaultProp.load(reader);
}

System.out.println("default properties for: " + defaultProp.getProperty("defaultProperty_username"));
System.out.println("default properties for: " + defaultProp.getProperty("defaultProperty_passwd"));

}

/**
* reading and loading from default properties file.
*
* @throws IOException
*/
public void customizeProperties() throws IOException {

System.out.println("customized properties for: " + applicationCustomized.getProperty("defaultProperty_username"));
System.out.println("customized properties for: " + applicationCustomized.getProperty("defaultProperty_passwd"));

applicationCustomized.setProperty("defaultProperty_username", "Updated_username");
applicationCustomized.setProperty("defaultProperty_passwd", "Updated_passwd");

try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("./resources/updatedFile.properties"))) {
defaultProp.store(writer,"Another comment for updated version");
}
}

public static void main(String[] args) throws IOException {
PracticeDefaultProperties p = new PracticeDefaultProperties();
p.propertiesLoader();
p.customizeProperties();

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s