Java Properties Class and read. properties file

Source: Internet
Author: User

I. The role of the. Properties file

  The Properties property file is a frequently visible and particularly important type of file in Java applications. It is used to configure the application of some information , but this information is generally relatively small data, there is no need to use database files to save, and use a generic text file to save, if it is directly saved through file, it may not be convenient for storage and reading, However, if the Save as properties file is different, the property file has a key value corresponding to the Java package, there is a specialized operation properties file class. This class is the Java.uitl.Properties class, because the properties class is a collection class , and property will read and write attributes as a collection. (There are two types of configuration files: XML and. properties files)

  In Java, the content format of the. Properties file is the "key-value" format, and the comment information for the text can be annotated with "#".

Second, Java Properties class

The properties class inherits from Hashtable, as follows:

The main methods it provides:

    1. GetProperty (String key): Searches for properties in this property list with the specified key. that is, the corresponding value is obtained by key.
    2. Load (InputStream instream): obtains (key-value) pairs for GetProperty () calls by loading the specified file .
    3. SetProperty (String key,string value): Call the base class's put method to set the key-value pair
    4. Store (OutputStream out,string comments): Writes key-value pairs to the specified file in the format of the file
    5. Clear (): Clears all reprinted key-value pairs

Third, read the. properties file

Db.properties

url = "Jdbc:mysql://localhost:3306/test"username= "root"name= "Root"

The first: The getResourceAsStream method using ServletContext: Returns the read byte stream of the resource file

 Public  void throws ioexception{        inputstreamin =this. Getservletcontext (). getResourceAsStream ("/web-inf/ Classes/db.properties ");        Properties Props=new  properties ();        Props.load (in);        String URL=props.getproperty ("url");        String username=props.getproperty ("username");        String Password=props.getproperty ("password");                System.out.println (URL);        SYSTEM.OUT.PRINTLN (username);        SYSTEM.OUT.PRINTLN (password);    }

The second type: Use ServletContext's Getrealpath method to get the full absolute path of the file, and then use the byte stream to read the file under Path

 Public voidTest2 ()throwsioexception{String Path= This. Getservletcontext (). Getrealpath ("/web-inf/classes/db.properties"); FileInputStream in=NewFileInputStream (path); ////the advantage of comparing the first method is that you can get the name of the resource file in addition to the data .String filename=path.substring (path.lastindexof ("\ \") +1);        SYSTEM.OUT.PRINTLN (filename); Properties Props=NewProperties ();        Props.load (in); String URL=props.getproperty ("url"); String username=props.getproperty ("username"); String Password=props.getproperty ("Password");        System.out.println (URL);        SYSTEM.OUT.PRINTLN (username);    SYSTEM.OUT.PRINTLN (password); }

Java Properties Class and read. properties file

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.