Introduction to properties file and its common Java operations

Source: Internet
Author: User
Tags list of attributes

Introduction of the Properties file

The properties file in Java is a configuration file, mainly used to express configuration information, the file type is *.properties, the format is a text file, the content of the file is the format is "key = value" (recommended) or "key: Value" format, In the properties file, you can use "#" (recommended) or "//" to make comments, properties files in Java programming in a lot of places, easy to operate.

Now define a databaseinfo.properties file, as follows:
#################################
# Financial System configuration file
# November 11, 2014
#################################
# Oracle Driver
Driver=oracle.jdbc.driver.oracledriver
# Oralc Database Address
Url=jdbc:oracle:thin:@192.168.2.3:1521:orcl
# User Name
Username=cwdb
# password
Password=cwdb
#################################

Precautions:
When we edit Chinese characters in the properties file, we will prompt the problem that cannot be saved, because the default encoding format of the properties resource file in MyEclipse is iso-8859-1, you need to modify the default encoding format. Open the MyEclipse window->perferences->general->contenttypesand find the Text->java Properties File Select, change the following Default encoding to:UTF-8, then click the Update button on the right, then click the OK button,

Ii. important methods of the properties class
The Properties class exists in package Java.util, which inherits from Hashtable (that is: publicclass Java.util.Properties extends java.util.hashtable{...} )
1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. load (InputStream instream)to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file (for example, the Databaseinfo.properties file above). To search for GetProperty (String key).

3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.
4. Store (OutputStream out, String comments)writes the list of attributes (key and element pairs) in this properties table to the output stream, in a format that is appropriate for loading into the properties table using the Load method. In contrast to the load method, the method writes a key-value pair to the specified file.

5. Clear ()clears all loaded key-value pairs. This method is provided in the base class.

Third, Java's common operation to the Properites class

1 ImportJava.io.FileOutputStream;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 ImportJava.io.OutputStream;5 ImportJava.util.Iterator;6 Importjava.util.Properties;7 ImportJava.util.Set;8 9 /**Ten * @Description: Common use of Java for properites classes One  */ A  Public classPropdemo { -  -     //read value by key the      Publicstring ReadValue (String FilePath, String key) { -Properties props =NewProperties (); -         Try { -InputStream in = This. GetClass (). getResourceAsStream ("/" +FilePath); + props.load (in); -String value =Props.getproperty (key); +SYSTEM.OUT.PRINTLN (key + "=" +value); A              at in.close (); -             returnvalue; -}Catch(Exception e) { - e.printstacktrace (); -             return NULL; -         } in     } -  to     //Write properties Information +      Public voidwriteproperties (String FilePath, String parametername, String parametervalue) { -Properties prop =NewProperties (); the         Try { *InputStream FIS = This. GetClass (). getResourceAsStream ("/" +FilePath); $             //read the list of attributes from the input stream (key-value pairs)Panax Notoginseng prop.load (FIS); -              theOutputStream fos=NewFileOutputStream ( This. GetClass (). GetResource ("/" +filePath). GetPath ()); +             //set a new key-value pair A Prop.setproperty (parametername, parametervalue); the             //writes the list of attributes (key and element pairs) in this properties table to the output stream +Prop.store (FOS, "Add key: '" + parametername + "' Value:" +parametervalue); -              $ fis.close (); $ fos.close (); -}Catch(IOException e) { - e.printstacktrace (); the         } -     }Wuyi      the     //Read all information about the properties -      Public voidreadproperties (String filePath) { WuProperties props =NewProperties (); -         Try { AboutInputStream in = This. GetClass (). getResourceAsStream ("/" +FilePath); $ props.load (in); -              -             //use Set collection to get all key values -Set KeyValue =Props.keyset (); A             //using while loop traversal +Iterator it =keyvalue.iterator (); the              while(It.hasnext ()) { -String key =(String) It.next (); $String property =Props.getproperty (key); theSYSTEM.OUT.PRINTLN (key + "=" +Property ); the             } the in.close (); the}Catch(Exception e) { - e.printstacktrace (); in         } the     } the  About      Public Static voidMain (string[] args) { the          the         //read value by key the         NewPropdemo (). ReadValue ("databaseinfo.properties", "url"); +          -         //Write properties Information the         NewPropdemo (). WriteProperties ("Databaseinfo.properties", "DatabaseType", "Oracle");Bayi          the         //Read all information about the properties the         NewPropdemo (). ReadProperties ("Databaseinfo.properties"); -     } -}

Precautions:
Java properties files are generally placed under classpath, so that the program can be easily read, about Classpath is actually the Java class or library storage path, that is, Java bytecode. class file storage path. In Java Engineering, the properties and class files are put together. In Web engineering, the easiest way to do this is to put it in the web-inf\classes directory of your web app or under another folder.

Introduction to properties file and its common Java operations

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.