Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a properties file in Java? [duplicate]

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?

like image 729
dean Avatar asked Jun 06 '11 06:06

dean


People also ask

What happens if we duplicate the key in Java?

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.

How do I read the URL of a property file?

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.


5 Answers

   . . .
   // 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. - ...

like image 127
Markus Lausberg Avatar answered Oct 16 '22 12:10

Markus Lausberg


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.

like image 5
Piotr Avatar answered Oct 16 '22 11:10

Piotr


You can use java.util.Properties

like image 3
trutheality Avatar answered Oct 16 '22 12:10

trutheality


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

  1. Non Static

    InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties");

  2. 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/

like image 3
Hitesh Garg Avatar answered Oct 16 '22 13:10

Hitesh Garg


InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
Properties p = new Properties();
p.load(in);
in.close();
like image 2
Marcin Michalski Avatar answered Oct 16 '22 13:10

Marcin Michalski