today do a read the properties file in the configuration, excerpt a little online example.
Finally succeeded, wrote down, to make a memorial
/*** @ Author WHS * @ Creation Date January 8, 2015 * @ Version V 1.0*/ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.util.Enumeration;Importjava.util.Properties; Public classTest {/*you can call the class name directly to read the contents of the Properties property file*/ Public voidInitfile () {String URL= This. GetClass (). getClassLoader (). GetResource ("A.properties"). toString (). SUBSTRING (6); String Empurl= Url.replace ("%20", "" "); //If your file path contains spaces, you will be sure to get an error.File File =NewFile (Empurl); FileInputStream FIS=NULL; Try { /*input stream and property file associations*/FIS=Newfileinputstream (file); /*To Create an attribute set object*/Properties prop=NewProperties (); /*loads the read content into the property set object*/prop.load (FIS); /*returns an enumeration of all keys in a property list*/Enumeration<?> enums =Prop.propertynames (); while(Enums.hasmoreelements ()) {/*cast each property to a string type to get the key*/String Key=(String) enums.nextelement (); /*gets the corresponding value according to the key (string type)*/String Value=Prop.getproperty (key); /*output the contents of the Properties property file*/System.out.println (Key+ "----" +value); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(FIS! =NULL) Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } } } Public Static voidMain (string[] args) {/*calling a method to read the contents of a property file*/test test=NewTest (); Test.initfile (); }}
several common ways to read. properties files in online articles
1. Example of the load () method using the Java.util.Properties class :
Java code
- InputStream in = new Bufferedinputstream (new FileInputStream (name));
- Properties P = new properties ();
- P.load (in);
InputStream in = new Bufferedinputstream (new FileInputStream (name)); Properties P = new properties (); P.load (in);
2. Example of the Getbundle () method using the Java.util.ResourceBundle class :
Java code
- ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
Use ResourceBundle to read. properties files to avoid path problems
When I read the. properties file in the jar, I always can't find the file path and later read the. properties file with ResourceBundle to avoid the path problem, the code is as follows:
Process is the file name, remember not to add. Properties, URL is the key name in the file
Java code
- ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process");
- String s = bundle.getstring ("URL");
- System.out.println (s);
- PURL = s;
ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process"); String s = bundle.getstring ("URL"); System.out.println (s); PURL = s;
3. Examples of constructors using the Java.util.PropertyResourceBundle class :
Java code
- InputStream in = new Bufferedinputstream (new FileInputStream (name));
- ResourceBundle RB = new propertyResourceBundle (in);
4. Example of the getResourceAsStream () method using class variables:
Java code
- InputStream in = class name. Class.getresourceasstream (name);
- Properties P = new properties ();
- P.load (in);
5. Example of the Java.lang.ClassLoader getResourceAsStream () method obtained using Class.getclassloader () :
Java code
- InputStream in = class name. Class.getclassloader (). getResourceAsStream (name);
- Properties P = new properties ();
- P.load (in);
6. Examples of Getsystemresourceasstream () static methods using the Java.lang.ClassLoader class :
Java code
- InputStream in = Classloader.getsystemresourceasstream (name);
- Properties P = new properties ();
- P.load (in);
7. The Javax.servlet.ServletContext getResourceAsStream () method can be used in the servlet example:
Java code
- InputStream in = context.getresourceasstream (path);
- Properties P = new properties ();
- P.load (in);
Java Read Properties configuration file