Java reads the properties file in the following six ways:
1. Using the Load () method of the Java.util.Properties class
String filename= "E:/system.properties"; InputStream in = new Bufferedinputstream (new FileInputStream (FileName)); Properties P = new properties (); P.load (in); SYSTEM.OUT.PRINTLN (P);
2. Using the Getbundle () method of the Java.util.ResourceBundle class
ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
ResourceBundle provides a shortcut to the internationalization of the software, which is typically used to read a resource properties file, 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.
Note : The name of this properties file is canonical: The generic naming convention is: Custom Name _ language code _ Country code. Properties,
If it is the default, write directly as: custom name. Properties
Like what:
Myres_en_us.properties
Myres_zh_cn.properties
Myres.properties
When the Chinese operating system, if the myres_zh_cn.properties, myres.properties two files are present, the priority is to use myres_zh_cn.properties, when Myres_zh_ Cn.properties does not exist, the default myres.properties is used.
Example: Define three resource files and place them below the root of src:
Myres.properties
Aaa=good
Bbb=thanks
Myres_en_us.properties
Aaa=good
Bbb=thanks
Myres_zh_cn.properties
aaa=\u597d
Bbb=\u591a\u8c22
import java.util.locale; import java.util.resourcebundle; public class Testresourcebundle { public static void main (String[] args) { locale locale1 = new locale ("zh", "CN"); ResourceBundle Resb1 = resourcebundle.getbundle ("Myres", locale1); system.out.println (resb1.getString ("AAA")); resourcebundle resb2 = resourcebundle.getbundle ("Myres", locale.getdefault ()); &Nbsp; system.out.println (resb2.getstring ("AAA")); locale locale3 = new locale ("en", "US"); resourcebundle resb3 = resourcebundle.getbundle ("Myres",  LOCALE3); system.out.println (resb3.getstring ("AAA")); } }
Results:
Good
Good
Good
3. Use the constructor of the Java.util.PropertyResourceBundle class
InputStream in = new Bufferedinputstream (new FileInputStream (name)); ResourceBundle RB = new propertyResourceBundle (in);
propertyResourceBundle is a specific subclass of ResourceBundle, which is a language environment resource through the static string management of a property file. Unlike other resource bundle types, you cannot create subclasses for propertyResourceBundle. Instead, provide a property file that contains the resource data. Resourcebundle.getbundle will automatically find the appropriate property file and create a propertyresourcebundle that references the file
4. The getResourceAsStream () method of the class variable using the Java.lang package
InputStream in = class name. Class.getresourceasstream (name); Properties p = new properties ();p. Load (in);
Cases:
Import java.io.bufferedinputstream;import java.io.file;import java.io.fileinputstream;import java.io.InputStream;import java.util.Properties;public class FileGet { private final static string sysfile= "/system.properties"; public static void main (String[] args) throws Exception { file file = new file (( FileGet.class.getResource (Sysfile)). GetFile ()); inputstream in = new bufferedinputstream (New fileinputstream (file)); properties p = new properties (); p.load (in); system.out.println (P); iNputstream in2 = fileget.class.getresourceasstream (Sysfile); properties p2 = new properties (); p2.load (in2); system.out.println (p2); }}
GetResource returns the URL object for the java.net package, getResourceAsStream returns the Java.io package InputStream
Example 2:
650) this.width=650; "src=" http://img.blog.csdn.net/20160721162609704 "alt=" here write a picture describing "title=" "/>
In the above directory, there is a src directory, then, we should in the test class how to get
Where file.txt can be obtained in these two ways:
Method One:
File File1 = new file (Test.class.getResource ("file.txt"). GetFile ());
Method Two:
File File2 = new file (Test.class.getResource ("/com/file.txt"). GetFile ());
File2.txt Get Method:
File File3 = new file (Test.class.getResource ("/file2.txt"). GetFile ());
Gets the different paths under which the file passed in is not the same parameter. When an incoming parameter is not "/", it gets the corresponding file under the package that contains the current class. When the parameter has "/", the file is obtained from the classpath root directory. The essence of this method is simply to construct an absolute path through the path, and ultimately the resource is obtained by ClassLoader.
5. The getResourceAsStream () method of Java.lang.ClassLoader obtained using Class.getclassloader ()
Example:
InputStream in = class name. Class.getclassloader (). getResourceAsStream (name); Properties p = new properties ();p. Load (in);
6. Using the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class
Example:
InputStream in = Classloader.getsystemresourceasstream (name); Properties p = new properties ();p. Load (in);
Add
The getResourceAsStream () method of Javax.servlet.ServletContext can be used in Servlets
Example:
InputStream in = context.getresourceasstream (path);
Properties P = new properties ();
P.load (in);
This article from the "10916470" blog, reproduced please contact the author!
Six ways Java reads the properties file