Properties class in JavaThe properties object contains key and value pair both as a string. The java.util.Properties class is the subclass of Hashtable. It can be used to get property value based on the property key. The Properties class provides methods to get data from the properties file and store data into the properties file. Moreover, it can be used to get the properties of a system. An Advantage of the properties fileRecompilation is not required if the information is changed from a properties file: If any information is changed from the properties file, you don't need to recompile the java class. It is used to store information which is to be changed frequently. Constructors of Properties class
Methods of Properties classThe commonly used methods of Properties class are given below.
Example of Properties class to get information from the properties fileTo get information from the properties file, create the properties file first. db.propertiesNow, let's create the java class to read the data from the properties file. Test.javaOutput:system oracle Now if you change the value of the properties file, you don't need to recompile the java class. That means no maintenance problem. Example of Properties class to get all the system propertiesBy System.getProperties() method we can get all the properties of the system. Let's create the class that gets information from the system properties. Test.javaOutput: java.runtime.name = Java(TM) SE Runtime Environment sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin java.vm.version = 21.1-b02 java.vm.vendor = Oracle Corporation java.vendor.url = http://java.oracle.com/ path.separator = ; java.vm.name = Java HotSpot(TM) Client VM file.encoding.pkg = sun.io user.country = US user.script = sun.java.launcher = SUN_STANDARD ........... Example of Properties class to create the properties fileNow let's write the code to create the properties file. Test.javaLet's see the generated properties file. info.propertiesNext TopicArrayList vs Vector |