[Java] Properties configuration information class

Properties configuration information

Properties is a subclass of HashTable, which is used to process property files

Since the Key and Value of the property file are all of the string type, the Key and Value in the Properties are also String

Use when accessing data:

setProperty(String k,String v)

getProperty(String k)

 

Pay attention to the configuration file, xxx.properties should be placed in the current project directory

 

You can see that the file information can be read

 

 The exception of the file stream and the exception of the loading method are directly handled by throwing

1  public  class PropertiesTest {
 2      public  static  void main (String [] args) throws IOException {
 3          // Create configuration information object 
4          Properties properties = new Properties ();
 5  
6          // Create file stream object, specify file 
7          FileInputStream inputStream = new FileInputStream ("jdbc.properties" );
 8  
9          // Load stream object information 
10          properties.load (inputStream);
 11  
12          // Read configuration information 
13         String username = properties.getProperty("username");
14         System.out.println(username);
15 
16         String password = properties.getProperty("password");
17         System.out.println(password);
18     }
19 }

 

Properties file

username = root
password = 123456
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mysql

 

Guess you like

Origin www.cnblogs.com/mindzone/p/12743479.html