In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to parse the Java loading property file configuration process, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
1 introduction to properties:
Properties is a text file with a format of:
Key = value # single-line comment
It is suitable for use as a simple configuration file, usually as a parameter configuration and internationalization resource file.
For complex configurations, you need to use XML, YML, JSON, etc.
2 java load Properties:
Java loads properties mainly through two utility classes under the util package: Properties class and ResourceBundle class
2.1 load through the Properties class:
The Properties class loads the configuration information through the load () method, which receives an input stream parameter: InputStream, Reader.
Properties provides a get (String key) method to read the specified configuration item.
2.1.1 obtain InputStream loading through ClassLoader:
This method can only read the configuration files under the classpath.
(1) create a Properties object first
(2) obtain the input stream in corresponding to the property file
(3) use Properties to load input stream in
(4) obtain the configuration through the Properties.get (key) method. If the configuration information does not exist, return null.
/ * * read properties; based on ClassLoader * this method can only read the configuration file under the classpath, but it is more convenient if the configuration file is under the classpath. * / public static void method1 () {System.out.println ("load properties using ClassLoader"); Properties properties = new Properties (); / * * use ClassLoader to load the properties configuration file to generate the corresponding input stream. * in this way, the property file must be placed in the src directory, and after compilation, it will be placed in the same directory as the class file. * because the basic path of ClassLoad is relative to the directory where the compiled class file is located (possibly bin,classes), if you put properties in the project root directory, you may not find the properties file in this way * / / you must start with /, look for InputStream in = PropertiesDemo.class.getResourceAsStream ("/ properties/a.properties") in the root directory of the classes file. System.out.println (PropertiesDemo.class.getClassLoader (). GetResource ("."). GetPath (); try {properties.load (in);} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} finally {closeResource (in);} System.out.println (properties.get ("name")); System.out.println (properties.get ("age"));}
2.1.2 load through building Reader:
The advantage of this method is that it can read the configuration file under any path.
(1) create a Properties object
(2) create a Reader. You can specify the path, not limited to the classpath.
(3) use Properties to load Reader
(4) Properties.get (key) method to read the configuration
/ * * use inputStream to read the configuration file * the advantage of this method is that it can read the configuration file under any path * / public static void method2 () {System.out.println ("load properties using InputStream"); Properties properties = new Properties (); BufferedReader reader = null Try {/ * * System.getProperty ("user.dir"): get the project root path, and then attach the configuration file path * / reader = new BufferedReader (new FileReader ("user.dir") + "/ properties/a.properties"); properties.load (reader); System.out.println (properties.get ("name")); System.out.println (properties.get ("age")) } catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} finally {closeResource (reader);}}
2.2 load through the ResourceBundle class:
There are two ways for ResourceBundle to read configurations:
(1) specify the file path: relative to src and classes
(2) provide InputStream
ResourceBundle provides methods getString (key) and getObject (key) to read configuration items using path loading, without specifying the file suffix name, and does not need to close related resources manually, which is simpler than Properties class operation.
/ * read the configuration through ResourceBundle. In this way, the project Properties should be simple * there are two ways for ResourceBundle to read the configuration: (1) specify the file path (2) provide InputStream * / public static void method3 () {System.out.println ("load properties using ResourceBundle") / * * (1) specify the file path directly: * you can use a relative path, starting from the classpath, without specifying the suffix of the properties file * / ResourceBundle resource = ResourceBundle.getBundle ("properties/b"); System.out.println (resource.getString ("name")); System.out.println (resource.getString ("age")) Try {/ * * (2) load configuration via InputStream: * get the resource input stream through the class instance of the current class * / resource = new PropertyResourceBundle (PropertiesDemo.class.getResourceAsStream ("/ properties/a.properties")); System.out.println (resource.getString ("name")); System.out.println (resource.getString ("age"));} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
The above content is how to parse the Java loading property file configuration process, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.