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:
- GetProperty (String key): Searches for properties in this property list with the specified key. that is, the corresponding value is obtained by key.
- Load (InputStream instream): obtains (key-value) pairs for GetProperty () calls by loading the specified file .
- SetProperty (String key,string value): Call the base class's put method to set the key-value pair
- Store (OutputStream out,string comments): Writes key-value pairs to the specified file in the format of the file
- 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