The difference between ResourceBundle and properties in Java

Source: Internet
Author: User

The first approach
InputStream is = Test.class.getResourceAsStream ("dbconfig.properties");
Properties P = new properties ();
P.load (IS);
System.out.println (P.get ("Jndi"));
Test.class to be placed in the same directory as the Dbconfig.properties??? This sentence needs to be tested.
The second approach
Private String Bundle_name = "com.newland.alarmquery.resource.DbConfig";////Do not add extension
ResourceBundle Resource_bundle = Resourcebundle.getbundle (bundle_name);
Jndi=resource_bundle.getstring ("Jndi");
System.out.println (P.get ("Jndi"));
In general, the ResourceBundle class is typically a property file for use in different languages.
And if the properties file in your application is just some configuration, it's not for multi-lingual purposes. Then you can use the Properties class.
You can usually put these property files in a jar file. Then, by calling the class's getResourceAsStream method, the stream object of the property file is obtained, and then loaded with the properties class's Load method.
Sometimes some simple configuration file can not use XML, in fact, the ResourceBundle class has done very well. It can even search for some properties files in the jar file in Classpath.
For example, place a file in the root directory of the jar file: Dbconfig.properties, and then as long as the jar file is in classpath. You can use a statement like this to get some properties

"A place to be aware of:"
The parameter arg0 in 1.resourcebundle.getbundle (String arg0) must contain the full path to the property file.
2. Combine the properties file (for example, config.properties) with the read file;
Property files and read files to be put together, if you want to separate the two files? Then we can ourselves create a directory, put the property file below it, and then set the directory to ClassLoader loaded directory (join the classpath) can be, as follows:
1, under the project to build a folder, the path arbitrary, the name arbitrary (this is: properties).
2. (in Eclipse) Select Project->properties->java Build path->libraries->add
Class Folder, add the properties file to the Classpath (or add it manually in the. classpath file: <classpathentry kind= "Lib" path= "Properties"/>). And then directly with
Resourcebundle.getbundle ("config"), it can read
The contents of the Properties/config.properties file.
The parameter passed in 3.resourcebundle.getbundle (ARGS0) is the basename of the resource file. And do not add the. properties file suffix.
If the resource file name is: Myres_zh_cn.properties or myres.properties, you only need to pass in Myres.
4.ResourceBundle the function of this class is to read the resource properties file (property), Then, depending on the name information (localization information) of the. properties file, match the country language information for the current system (or program-specified), and then get the contents of the corresponding properties file.

The difference between ResourceBundle and properties in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.