Read a Properties file in Java
Share
Project configuration data or settings are store in Properties file. Here we look how to read a properties file in Java.
The java.util.Properties
class is the subclass of Hashtable and represents a persistent set of properties. .properties
is a file extension used in Java.
Properties file contains key and value pair. Each key and its corresponding value in the property list is a string.
Moreover, java.util.Properties
class can be used to get the properties of a system.
The Properties class represents a persistent set of properties that can be loaded from a stream using its load() method.
Multiple ways to read a properties file in java
FileInputStream
The FileInputStream obtains input bytes from a file in a file system. It is a descended from the abstract class InputStream, which is a super types of all input byte streams.
InputStream fis = new FileInputStream("Prop.properties");
FileInputStream with direct file name would read the files which are at file system rather than at classpath. In above example prop.properties
is at project file system level look at Fig 1.1/1.2.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ReadProperties { public ReadProperties() { try { //File located @ fiel system and not on the classpath FileInputStream fis = new FileInputStream("Prop.properties"); Properties prop = new Properties(); prop.load(fis); System.out.print(prop.getProperty("url")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
The output would be as below.
Similarly, if you want to load file from classpath using FileInputStream
Let us now look into other ways with file placed at different places w.r.t classpath. Firstly, lets look into file (project) structure (placement of files).
ClassLoader / Class
ClassLoader is an abstract class, used to load the classes at run time dynamically to JVM. When we request to load a class, it delegates the class to its parent.
Classes would not load into memory all at once, but on demand by an application. At this point, the Java ClassLoader is called by the JRE to load classes into memory dynamically.
For instance where method is a static method, ClassLoader syntax would be ClassLoader loader =<classname>.class.getClassLoader();
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties { public static void readProperties() { try { ClassLoader loader = ReadProperties.class.getClassLoader(); InputStream inputStream = loader.getResourceAsStream("Prop.properties"); Properties prop = new Properties(); prop.load(inputStream); System.out.print(prop.getProperty("url")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Output: Properties file at Parent class path
Similarly if you read “Config.properties” loader.getResourceAsStream("Config.properties");
file the the output would be as below.
Output: Properties file at class path
This method of reading file help you to read the files at main and sub folders of Resources(Ref: Fig 2)
Similarly, loader.getResourceAsStream("ConfigRes.properties");
and loader.getResourceAsStream("prop/ConfigProp.properties");
provide outputs as below respectively.
Output: Properties file in Resources Output: Properties file in Sub folder of Resources
However if the method is not a static one, then syntax would be ClassLoader loader = this.getClass().getClassLoader();
.
public ReadProperties() { try { ClassLoader loader = this.getClass().getClassLoader(); InputStream inputStream = loader.getResourceAsStream("prop/ConfigProp.properties"); Properties prop = new Properties(); prop.load(inputStream); System.out.print(prop.getProperty("url")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
Otherwise you can directly use getResourceAsStream() method of the Class object instead of ClassLoader class.
public ReadProperties() { try { InputStream inputStream = this.getClass().getResourceAsStream("Config.properties"); Properties prop = new Properties(); prop.load(inputStream); System.out.print(prop.getProperty("url")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
In addition, if the file is on the file system(Ref Fig 1.1) rather than on the classpath we can use the static method getSystemResourceAsStream()of the ClassLoader class.
public static void readProperties() { try { InputStream inputStream = ClassLoader.getSystemResourceAsStream("Prop.properties"); Properties prop = new Properties(); prop.load(inputStream); System.out.print(prop.getProperty("url")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
In our next post we look into how to read a properties file in java using ResourceBundle
and also third party Library Apache Commons Configuration
Also you can look into updating property value during runtime.