In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use J2SE to read Properties files, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
There are six ways to read Properties files using J2SE:
1. Use the load () method of the java.util.Properties class:
InputStream in = new BufferedInputStream (new FileInputStream (FILENAME)); Properties p = new Properties (); p.load (in)
two。 Use the getBundle () method of the java.util.ResourceBunld class:
ResourceBundle rb = ResourceBundle.ResourceBundle.getBundle (FILENAME,Locale.getDefault ())
3. Use PropertyResourceBundle
InputStream in = new BufferedInputStream (new FileInputStream (name)); ResourceBundle rb = new PropertyResourceBundle (in)
4. Use getResourceAsStream (String name) of the Class class
5. The getResourceAsStream () method of java.lang.ClassLoader obtained by using class.getClassLoader ()
6. Use the getSystemResourceAsStream () static method of the java.lang.ClassLoader class
The following is transferred from: http://blog.csdn.net/bincavin/archive/2010/03/09/5359047.aspx
In general, in a real project, in order to facilitate configuration management, we need to store some configuration management information in a .properties file and read it directly, thus avoiding a lot of hard coding. Next, through an example, explain in detail how to read the data from the .properties file.
1. Create a package config to store configuration files such as .properties, and create a file a.properties under the config package. To facilitate testing, add the following information under a.properties:
Name=kaka
Age=28
two。 Create a package prop for testing. Create the LoadProp.java file under the prop package.
3. There are many ways to read .properties files, and now the main methods are listed:
a. Read through the getResourceAsStream () method of class
Package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp {public static void main (String [] args) {LoadProp loadProp = new LoadProp (); InputStream in = loadProp.getClass () .getResourceAsStream ("/ config/a.properties"); Properties prop = new Properties (); try {prop.load (in) } catch (IOException e) {e.printStackTrace ();} System.out.println (prop.getProperty ("name", "none"); System.out.println (prop.getProperty ("age", "none"));}}
Java code
Package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp {public static void main (String [] args) {LoadProp loadProp = new LoadProp (); InputStream in = loadProp.getClass (). GetResourceAsStream ("/ config/a.properties"); Properties prop = new Properties (); try {prop.load (in);} catch (IOException e) {e.printStackTrace () } System.out.println (prop.getProperty ("name", "none")); System.out.println (prop.getProperty ("age", "none");}}
It is important to note that the classpath of the parameter in the getResourceAsStream () method in class must be preceded by "/", otherwise an error will be reported.
b. Use the getResourceAsStream () of ClassLoader obtained by the getClassLoader () method of class to read package prop
Java code
Import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp {public static void main (String [] args) {LoadProp loadProp = new LoadProp (); InputStream in = loadProp.getClass (). GetClassLoader (). GetResourceAsStream ("config/a.properties"); Properties prop = new Properties (); try {prop.load (in);} catch (IOException e) {e.printStackTrace () } System.out.println (prop.getProperty ("name", "none")); System.out.println (prop.getProperty ("age", "none");}}
Java code
Import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp {public static void main (String [] args) {LoadProp loadProp = new LoadProp (); InputStream in = loadProp.getClass (). GetClassLoader (). GetResourceAsStream ("config/a.properties"); Properties prop = new Properties (); try {prop.load (in) } catch (IOException e) {e.printStackTrace ()} System.out.println (prop.getProperty ("name", "none"); System.out.println (prop.getProperty ("age", "none"));}}
ClassLoader's getResourceAsStream () method is a little different from Class's getResourceAsStream () method. It's strange not to add "/" in front of the classpath here, or else an error will be reported.
c. Use ResourceBundle to read
Java code
Package prop; import java.util.ResourceBundle; public class LoadProp {public static void main (String [] args) {ResourceBundle rb = ResourceBundle.getBundle ("config/a"); System.out.println (rb.getString ("name")); System.out.println (rb.getString ("age"));}}
Java code
Package prop; import java.util.ResourceBundle; public class LoadProp {public static void main (String [] args) {ResourceBundle rb = ResourceBundle.getBundle ("config/a"); System.out.println (rb.getString ("name")); System.out.println (rb.getString ("age"));}}
Note that the parameter in the getBundle () method is baseName, do not write the suffix name, and do not add "/".
Well, these are the main ways to read .properties files, and there are others, which are pretty much the same. In addition, you can use the getResourceAsStream () method of javax.servlet.ServletContext to read .properties files in Servlet, which is not detailed here.
The above is all the contents of the article "how to use J2SE to read Properties files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.