Both classes can read the key-value pairs stored in the form of key/value in the property file. When ResourceBundle reads the property file The operation is relatively simple.
This class inherits Hashtable and stores key-value pairs in a collection. Based on the input stream, key-value pairs are read from the property file. After the load() method is called, it is disconnected from the input stream and will not automatically close the input stream. It needs to be closed manually.
/** * 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中, * 通过put方法可以向集合中添加键值对或者修改key对应的value * * @throws IOException */@SuppressWarnings("rawtypes") @Testpublic void test01() throws IOException { FileInputStream fis = new FileInputStream("Files/test01.properties"); Properties props = new Properties(); props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭/*byte[] buf = new byte[1024]; int length = fis.read(buf); System.out.println("content=" + new String(buf, 0, length));//抛出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"));/** * Properties其他可能用到的方法 */props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)props.put("jdbc.password", "456"); FileOutputStream fos = new FileOutputStream("Files/test02.xml");// 将Hashtable中的数据写入xml文件中props.storeToXML(fos, "来自属性文件的数据库连接四要素"); System.out.println(); System.out.println("遍历属性文件"); System.out.println("hashtable中键值对数目=" + props.size()); Enumeration keys = props.propertyNames();while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); System.out.println(key + "=" + props.getProperty(key)); } }
This class reads the property file based on the class: treat the property file as a class, This means that the properties file must be placed in the package, and the properties file is referred to using its fully qualified class name rather than its path.
/** * 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件 */@Testpublic 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)); } }
Usually the four elements of database connection are placed in the properties file , the program reads parameters from the property file, so that when the database connection elements change, does not need to modify the source code. Method to load the content in the properties file into the xml document:
Configure context constraints in the configuration file header.
Add
Get configuration file content: ${key}
# placed in front is used to add comments in the properties file.
The attribute file adopts ISO-8859-1 encoding method. This encoding method does not support Chinese and Chinese characters will be converted. Displayed in Unicode encoding mode.
The above is the detailed content of Detailed introduction to Properties and ResourceBundle. For more information, please follow other related articles on the PHP Chinese website!