I want to create a config.properties file, in which I want to store all the key and values instead of hard coding them in the Java code.
However, I do not know how to create a properties file in eclipse. I researched and found help on how to read a properties file. I need help with how to create it.
Here are my specific questions:
config.properties
file be created in eclipse, and data be
typed directly into it as though the config.properties is similar to
text editor?I will greatly appreciate any help.
Create a properties fileRight-click and select Add New Properties File. A new properties file will be added to your project. The new file will be selected and highlighted in the list. Type a name for your properties file, for example, "Properties".
properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.
Then you can add properties your property file like this
dbpassword=password
database=localhost
dbuser=user
Example of loading properties
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With