In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how java gets the contents of properties files dynamically in real time. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Real-time dynamic acquisition of the contents of properties files by java
When you read the properties file with "ClassLoader.getResourceAsStream", you will find that after the .properties has been modified, even if it is re-executed, the parameters read are still the parameters before the modification.
The cause of this problem is that after the ClassLoader.getResourceAsStream is read in, the .properties is saved in the cache and read from the cache when re-executed instead of reading the .properties file again.
The dynamically read code import java.util.Properties; / * acquires the value of properties file in real time * @ author Administrator * * / public class demo01 {/ * obtains the value in configuration file * @ param key configuration name * @ param filePath configuration file path name in real time according to configuration variables, for example: test.properties * @ return configuration value * / public static String getCurrentPropertiesValue (String key,String filePath) {String value= "" Properties p = new Properties (); try {/ / non-real-time dynamic acquisition / / p.load (new InputStreamReader (this.class.getClassLoader (). GetResourceAsStream (filePath), "UTF-8")); / / below is dynamic acquisition String path = Thread.currentThread (). GetContextClassLoader (). GetResource ("). GetPath (); InputStream is = new FileInputStream (path + File.separator+ filePath); p.load (is); value=p.getProperty (key) } catch (UnsupportedEncodingException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} return value;}} Java reads the configuration file Properties VS ResourceBundle
In java development, some commonly used configuration information is usually stored in the property file, because it is not necessary to recompile the jar package to modify the configuration file. For properties files, you can usually use Properties and ResourceBundle classes to parse. It should be noted that by default, the encoding format of the * .properties file in the java project is ISO-8859-1, and Properties and ResourceBundle also parse the strings in the property file according to the ISO-8859-1 format. Therefore, you need to pay extra attention to parsing familiar files that contain Chinese.
ResourceBundle: usually used to parse internationalized resource properties files, the corresponding internationalized resource is automatically selected according to the local environment.
Properties: used to parse common property files
1. Commonly used API
1.1 Properties commonly used API
Properties inherits the Hashtable class.
Method signature method description public String getProperty (String key gets the Key in the properties file. If the key does not exist, the value that returns Nullpublic String getProperty (String key, String defaultValue) to get the key object in the property file is returned. If the key does not exist, the method in the parent class HashTable of the default defaultValuepublic Object get (String key) is returned. The return type is Object.
1.2 ResourceBundle commonly used API
ResourceBundle is an interface that uses PropertyResourceBundle to parse property files by default.
Method signature method description public Locale getLocale () gets all the keypublic final String getString (String key) in the local internationalization environment public Enumeration getKeys () gets the value corresponding to the key in the property file, and the return value is String. If it does not exist, an exception public final Object getObject (String key) is thrown to get the value corresponding to the key in the property file, and the return value is Object. If it does not exist, an exception is thrown. Properties parses the property file
ISO-8859-1 is used by default to parse strings in the configuration file, so it will lead to Chinese garbled.
2.1 parsing English-only configuration files
/ / default encoding (ISO-8859-1) reads properties file, Chinese garbled @ Testpublic void test_properties_en () throws IOException {/ / property file location, relative path is src/main/resources or src/test/resources, cannot add classpath:/ prefix String propertyFileName = "jdbc.properties"; / / get byte stream InputStream is = getClass () .getResourceAsStream (propertyFileName) / / create a properties file and load the file contents Properties properties = new Properties (); properties.load (is); String username = properties.getProperty ("jdbc.username"); String password = properties.getProperty ("jdbc.password"); System.out.println ("username:" + username + ", password:" + password);}
2.2 parsing configuration files with Chinese
ISO-8859-1 is used by default, and InputStreamReader is used to convert to UTF8 character stream.
/ / specify the file encoding method to read, support reading @ Testpublic void test_properties_zh () throws IOException {/ / attribute file location in Chinese, relative path is src/main/resources or src/test/resources, cannot add classpath:/ prefix String propertyFileName = "jdbc.properties"; / / get byte stream InputStream is = getClass () .getClassLoader () .getResourceAsStream (propertyFileName) / convert to UTF-8 format character stream InputStreamReader isr = new InputStreamReader (is, "UTF-8"); / / create a properties file and load the file contents Properties properties = new Properties (); properties.load (isr); String username = properties.getProperty ("jdbc.username"); String password = properties.getProperty ("jdbc.password"); System.out.println ("username:" + username + ", password:" + password);} 3. ResourceBundle parses the property file
3.1 parsing English-only configuration files
@ Testpublic void testRb_en () {/ / Resource configuration file, without the need to write the file suffix, look for the properties file String bundleName = "jdbc" by default; / / set the local default environment to the English environment Locale.setDefault (Locale.ENGLISH); / / specify to load ResourceBundle rb = ResourceBundle.getBundle (bundleName); String username = rb.getString ("jdbc.username"); String password = rb.getString ("jdbc.password") System.out.println ("username:" + username + ", password:" + password);}
3.2 parsing configuration files containing Chinese
/ / deal with the Chinese @ Testpublic void testRb_zh () {/ / resource configuration file, without writing the file suffix, look for the properties file String bundleName = "jdbc" by default; / / load the resource configuration file ResourceBundle rb = ResourceBundle.getBundle (bundleName) according to the local default environment; String username = iso2Utf8 (rb.getString ("jdbc.username")); String password = iso2Utf8 (rb.getString ("jdbc.password")) System.out.println ("username:" + username + ", password:" + password);} / * @ Description iso encoded format string converted to UTF8 format * @ param str iso encoded string * @ return * @ author zongf * @ date 8 January 2019-3:55:29 * / private String iso2Utf8 (String str) {if (null = = str) return null Property file
The author creates a maven project and uses junit unit tests, so the author's configuration files are stored in the src/test/resources directory.
Jdbc.properties
Jdbc.username= Zhang San jdbc.password=123456
Jdbc_zh.properties
Jdbc.username= Zhang San jdbc.password=123456
Jdbc_en.properties
Jdbc.username=zhangsanjdbc.password=1234565. Actual combat recommendation
The author believes that a well-designed property configuration class should be a constant class, which should conform to at least two design principles:
Once the property is set and cannot be dynamically modified, the modification method cannot be called even in the compiled environment.
Can be accessed directly through class attributes, without having to be accessed through class objects
Automatically assemble attributes without having to manually parse familiar files (spring can be achieved with self-annotated or custom annotated)
Public class JdbcProperty {/ * user name * / public static final String username; / * user password * / public static final String password; / / in Spring applications, automatic assembly can be done with notes or custom annotations. The author only applies static {/ / load property file ResourceBundle resourceBundle = ResourceBundle.getBundle ("jdbc") for general java here; / / initialization attribute username = resourceBundle.getString ("jdbc.username") Password = resourceBundle.getString ("jdbc.password");}} Thank you for reading! This is the end of this article on "how java gets the contents of properties files dynamically in real time". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.