I have a list of key/value pairs of configuration values I want to store as Java property files, and later load and iterate through.
Questions:
Example:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("D:/prop/prop.properties");
pro.load(in);
String temp1[];
String temp2[];
// getting values from property file
String username = pro.getProperty("usernamev3");//key value in prop file
String password = pro.getProperty("passwordv3");//eg. username="zub"
String delimiter = ","; //password="abc"
temp1=username.split(delimiter);
temp2=password.split(delimiter);
1) It is good to have your property file in classpath but you can place it anywhere in project.
Below is how you load property file from classpath and read all properties.
Properties prop = new Properties();
InputStream input = null;
try {
String filename = "path to property file";
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2) Property files have the extension as .properties
in my opinion other ways are deprecated when we can do it very simple as below:
@PropertySource("classpath:application.properties")
public class SomeClass{
@Autowired
private Environment env;
public void readProperty() {
env.getProperty("language");
}
}
it is so simple but i think that's the best way!! Enjoy
There are many ways to create and read properties
files:
.properties
extension however you can choose your own.java.util
package => Properties
, ListResourceBundle
, ResourceBundle
classes.Properties
or java.lang.System
class.ResourceBundle
class:
ResourceBundle rb = ResourceBundle.getBundle("prop"); // prop.properties
System.out.println(rb.getString("key"));
Properties
class:
Properties ps = new Properties();
ps.Load(new java.io.FileInputStream("my.properties"));
This load the properties file:
Properties prop = new Properties();
InputStream stream = ...; //the stream to the file
try {
prop.load(stream);
} finally {
stream.close();
}
I use to put the .properties file in a directory where I have all the configuration files, I do not put it together with the class that accesses it, but there are no restrictions here.
For the name... I use .properties for verbosity sake, I don't think you should name it .properties if you don't want.
I have written on this property framework for the last year. It will provide of multiple ways to load properties, and have them strongly typed as well.
Have a look at http://sourceforge.net/projects/jhpropertiestyp/
JHPropertiesTyped will give the developer strongly typed properties. Easy to integrate in existing projects. Handled by a large series for property types. Gives the ability to one-line initialize properties via property IO implementations. Gives the developer the ability to create own property types and property io's. Web demo is also available, screenshots shown above. Also have a standard implementation for a web front end to manage properties, if you choose to use it.
Complete documentation, tutorial, javadoc, faq etc is a available on the project webpage.