I am using servlets where I hard-code the database connection details, so if make any change I have to recompile the code. So instead I'd like to use a .properties
file (which I can modify later) and use that as the source for my database connection.
The problem is I don't know how to read the property file. Could someone please help me to read the file?
As we can see, if we try to insert two values for the same key, the second value will be stored, while the first one will be dropped. It will also be returned (by every proper implementation of the put(K key, V value) method): Map<String, String> map = new HashMap<>(); assertThat(map.
Steps for reading a properties file in Java Create an instance of Properties class. Create a FileInputStream by opening a connection to the properties file. Read property list (key and element pairs) from the input stream using load() method of the Properties class.
. . .
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
. . .
Example is coming from here Properties
(Java)
The methods of Properties
can throw exceptions.
- When the file path is not valid (FileNotFoundException
). Please try to create a File
object and check, whether the File
is existing.
- ...
You may take a look at Apache Commons Configuration. Using it you can read properties file like that:
Configuration config = new PropertiesConfiguration("user.properties");
String connectionUrl = config.getString("connection.url");
This information regarding file location may be also important:
If you do not specify an absolute path, the file will be searched automatically in the following locations:
- in the current directory
- in the user home directory
- in the classpath
So in case of reading properties file in a servlet you should put properties file in a classpath (e.g. in WEB-INF/classes
).
You can find more examples at their website.
You can use java.util.Properties
The biggest problem in reading a property file in web application is that you actually don't know about the actaul path of the file. So we have to use the relative path and for that we have to use various functions and classes like getresourceAsStream(), InputStream, FileinputStream etc.
And the method getReourceAsStream behaves differently in static and non static methogs.. you can do this in below way
Non Static
InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties");
Static
InputStream input = ReadPropertyFile.class.getClassLoader().getResourceAsStream("config.properties");
For complete reference you can follow these links..
http://www.codingeek.com/java/using-getresourceasstream-in-static-method-reading-property-files
http://www.codingeek.com/java/read-and-write-properties-file-in-java-examples/
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
Properties p = new Properties();
p.load(in);
in.close();
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