InterviewSolution
| 1. |
What is the use of Properties class in Java? What is the advantage of the Properties file? |
|
Answer» The KEY and value pair are both strings in the properties object. The java.util.Properties class is a Hashtable subclass. It can be used to calculate the value of a property based on its key. The Properties class has methods for reading and writing data to and from the properties file. It can also be used to obtain a system's attributes. Advantage of the Properties file: If the INFORMATION in a properties file is modified, no recompilation is required: You don't need to recompile the java class if any information in the properties file changes. It is used to keep track of information that needs to be updated frequently. Example: Let us first create a properties file named “info.properties” having the following content : user = success PASSWORD = determination Let us now create a java class to read data from the properties file import java.util.*; import java.io.*; public class Sample { public static void main(String[] args)THROWS EXCEPTION{ FileReader reader = new FileReader("info.properties"); Properties obj_p = new Properties(); obj_p.load(reader); System.out.println(obj_p.getProperty("user")); System.out.println(obj_p.getProperty("password")); } }Output: successdetermination |
|