Read properties file and get property value

1.Properties与ResourceBundle

Both classes can read the key-value pairs stored in the property file in the form of key/value, and the operation of ResourceBundle reading the property file is relatively simple .

2.Properties

This class inherits Hashtable and stores key-value pairs in a collection. Read the key-value pair from the property file based on the input stream. After the load() method is called, it will be separated from the input stream, and the input stream will not be automatically closed, and it needs to be closed manually.

copy code
    /**
     * Read property files based on input stream: Properties inherit Hashtable, and the bottom layer stores key/value key-value pairs in the collection,
     * Through the put method, you can add key-value pairs to the collection or modify the value corresponding to the key
     *
     * @throws IOException */ @SuppressWarnings("rawtypes" ) @Test public void test01() throws IOException { FileInputStream fis = new FileInputStream("Files/test01.properties" ); Properties props = new Properties(); props.load( fis); // Read the entire content of the file into memory, the input stream reaches the end fis.close(); // After loading, the input stream is no longer used, the program is not actively closed, and needs to be closed manually /* byte[ ] buf = new byte[1024]; int length = fis.read(buf); System.out.println("content=" + new String(buf, 0, length));//Throw StringIndexOutOfBoundsException */ System. out.println("driver=" + props.getProperty("jdbc.driver")); System.out.println("url=" + props.getProperty("jdbc.url" )); System.out.println("username=" + props.getProperty("jdbc.username" )); System .out.println("password=" + props.getProperty("jdbc.password" )); /** * Other possible methods of Properties */ props.put("serverTimezone", "UTC"); // The bottom layer passes hashtable.put(key,value) props.put("jdbc.password", "456" ); FileOutputStream fos = new FileOutputStream("Files/test02.xml"); // Write the data in Hashtable to xml In the file props.storeToXML(fos, "Four elements of database connection from property file" ); System.out.println(); System.out.println("traverse property file" ); System.out.println("Number of key-value pairs in hashtable=" + props.size()); Enumeration keys = props.propertyNames();while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); System.out.println(key + "=" + props.getProperty(key)); } }
copy code

The following code can read the properties file through a relative path:

      Properties props= new Properties();
 //      Get the properties file stream through the class loader, the path is the relative path 
      InputStream in = PropertyUtil.class .getClassLoader ().getResourceAsStream(jdbc.properties);
      props.load(in);

 

3.ResourceBundle

This class reads properties files based on classes: treating properties files as classes, meaning properties files must be placed in a package, referring to properties files using the properties file's fully qualified class name rather than a path.

Just write the file name, not the file suffix.

copy code
    /**
     * Read property file based on class: This method treats the property file as a class, the property file is placed in the package, and the full qualification of the property file is used instead of the path to refer to the file
     */ @Test
    public void test02() { ResourceBundle bundle = ResourceBundle.getBundle("com.javase.properties.test01"); System.out.println("获取指定key的值"); System.out.println("driver=" + bundle.getString("jdbc.driver")); System.out.println("url=" + bundle.getString("jdbc.url")); System.out.println("username=" + bundle.getString("jdbc.username")); System.out.println("password=" + bundle.getString("jdbc.password")); System.out.println("-----------------------------"); System.out.println("遍历属性文件"); Enumeration<String> keys = bundle.getKeys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); System.out.println(key + "=" + bundle.getString(key)); } }
copy code
 

 Reference blog:

https://www.cnblogs.com/tonghun/p/7124245.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324998303&siteId=291194637