Java methods for reading properties files

Source: Internet
Author: User

Java methods for reading properties files

The properties file in java is a configuration file mainly used to express configuration information. The file type is *. properties is a text file whose content is in the format of key = value. In the properties file, # Can Be Used for comments, the properties file is widely used in Java programming and is easy to operate.

For example:

 

Test. properties -------------------------------------------------- ################################# IcisReport Application configuration File # Date: august 01, 2015 #################################### description: test # ipIcisReport of IcisReport. server. ip = 192.168.3.143 # IcisReport port IcisReport. server. port = 8080 # IcisReport context path IcisReport. contextPath =/IcisReport

Important methods of the Properties class
The Properties class exists in the cell Java. util and inherits from Hashtable.
1. getProperty (String key). Use the specified key to search for properties in this attribute list. That is, the value corresponding to the key is obtained through the key parameter.
2. load (InputStream inStream), read the attribute list (key and element pair) from the input stream ). You can load the specified file (such as the test. properties File above) to obtain all key-value pairs in the file. For getProperty (String key) search.
3. setProperty (String key, String value): Call the Hashtable method put. He sets key-value pairs by calling the put Method of the base class.
4. store (OutputStream out, String comments) writes the attribute list (key and element pair) in the Properties table to the output stream in a format suitable for loading to the Properties table using the load method. In contrast to the load method, this method writes a key-value pair to a specified file.
5. clear () to clear all loaded key-value pairs. This method is provided in the base class.

 

Properties file path: the java properties file must be placed under classpath so that the program can read the file. The classpath is actually the storage path of the java class or library. In the java project, put properties in the class file. In a web application, the simplest way is to put it in the web-INFclasses directory of the WEB application, or in other folders. In this case, when setting the classpath environment variable, add the folder path to the classpath variable, which can also be read.

Read:

1. absolute path of the properties file:

 

String path = C:\db.properties;FileInputStream in = new FileInputStream(path);Properties prop = new Properties();prop.load(in);prop.getProperty(IcisReport.server.ip);
2. Location of files in the web project $ app/WEB-INF/classes/db. properties

 

There are two scenarios on the web: Servlet and non-Servlet.

There are two methods in Servlet:

1,

 

InputStream in = this.getServletContext().getResourceAsStream(/WEB-INF/classes/db.properties);Properties prop = new Properties();prop.load(in);prop.getProperty(username);
2,

 

 

String path = this.getServletContext().getRealPath(/WEB-INF/classes/db.properties);FileInputStream in = new FileInputStream(path);Properties prop = new Properties();prop.load(in);prop.getProperty(username);
When a non-Servlet is used, assume that the class name is Demo and you want to use the Class Loader for reading. There are also two methods:
1. Directly load the file into the memory
InputStream in = Demo.class.getClassLoader().getResourceAsStream(db.properties);Properties prop = new Properties();prop.load(in);prop.getProperty(username);

 

 

This method Disadvantages : When the class loader loads content, it first finds whether the corresponding content already exists in the memory:If some files are not loaded, the memory is used directly. Therefore, after the first loading, if the file content changes, the modified content cannot be loaded after the second loading.
The second method below gets the absolute path of the file to load.

 

2. Obtain the absolute path of the file and perform the operation again.

String path = Demo.class.getClassLoader().getResource(db.properties).getPath();FileInputStream in = new FileInputStream(path);Properties prop = new Properties();prop.load(in);prop.getProperty(username);

The following is an example:

 

// Read value public static String readValue (String filePath, String key) {Properties props = new Properties (); try {InputStream in = new BufferedInputStream (new FileInputStream (filePath); props. load (in); String value = props. getProperty (key); System. out. println (key + value); return value;} catch (Exception e) {e. printStackTrace (); return null ;}// read all properties information public static void readPr Operties (String filePath) {Properties props = new Properties (); try {InputStream in = new BufferedInputStream (new FileInputStream (filePath); props. load (in); Enumeration en = props. propertyNames (); while (en. hasMoreElements () {String key = (String) en. nextElement (); String Property = props. getProperty (key); System. out. println (key + Property) ;}} catch (Exception e) {e. printStackTrace ();}} // Write the properties information public static void writeProperties (String filePath, String parameterName, String parameterValue) {Properties prop = new Properties (); try {InputStream FS = new FileInputStream (filePath ); // read the property list (key and element pair) prop from the input stream. load (FCM); // call the Hashtable method put. Use the getProperty method to provide parallelism. // The attribute key and value must use a string. The returned value is the result of calling put by Hashtable. OutputStream fos = new FileOutputStream (filePath); prop. setProperty (parameterName, parameterValue); // write the property list (key and element pair) in the Properties table to the output stream prop in a format suitable for loading to the Properties table using the load method. store (fos, Update' + parameterName + 'value);} catch (IOException e) {System. err. println (Visit + filePath + for updating + parameterName + value error);} public static void main (String [] args) {readValue (info. properties, url); writeProperties (info. properties, age, 21); readProperties (info. properties); System. out. println (OK );}





 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.