1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 ImportJava.io.InputStreamReader;5 Importjava.io.UnsupportedEncodingException;6 Importjava.util.Enumeration;7 ImportJava.util.Iterator;8 Importjava.util.Properties;9 ImportJava.util.Set;Ten One ImportJavax.servlet.ServletContext; A Importjavax.servlet.ServletException; - ImportJavax.servlet.annotation.WebServlet; - ImportJavax.servlet.http.HttpServlet; the -@WebServlet (urlpatterns={"/propertiesservlet"},loadonstartup=1) - Public classPropertiesservletextendsHttpServlet { - + Private Static Final LongSerialversionuid = 1L; - + PublicPropertiesservlet () { A Super(); at } - - Public voidInit ()throwsservletexception{ - - //Get ServletContext -ServletContext context = This. Getservletcontext (); in //Get ServletConfig - //ServletConfig servletconfig = This.getservletconfig (); to + //gets the name of all initialization parameters -Enumeration<string> Enumer =context.getinitparameternames (); the //enumeration<string> initparameternames = Servletconfig.getinitparameternames ();//also gets the name of all initialization parameters * //Enumer = initparameternames; $ Panax Notoginseng while(Enumer.hasmoreelements ()) { -String name =enumer.nextelement (); theString value = context.getinitparameter (name);//get parameter values by parameter name +SYSTEM.OUT.PRINTLN (name + ":" +value); A } the + - //three ways to get resource file input stream InputStream via ServletContext $ //URL url = context.getresource ("web-inf/classes/jdbc.properties"); $ //InputStream is1 = Url.openstream (); - - //String Path = Context.getrealpath ("web-inf/classes/jdbc.properties"); the //File File = new file (path); - //InputStream is2 = new FileInputStream (file);Wuyi theInputStream is3 = Context.getresourceasstream ("web-inf/classes/jdbc.properties");//Convenient - Wu //three ways to get ClassLoader - //ClassLoader classLoader1 = This.getclass (). getClassLoader (); About //ClassLoader classLoader2 = Thread.CurrentThread (). Getcontextclassloader (); $ //ClassLoader ClassLoader3 = Classloader.getsystemclassloader (); - - //get InputStream through ClassLoader - //InputStream is = Classloader3.getresourceasstream ("jdbc.properties"); A + //InputStream is4 = Classloader.getsystemresourceasstream ("jdbc.properties");//Convenient the -Properties prop =NewProperties (); $ the //Java encoding used in network transmission is "iso-8859-1", if the configuration file is in Chinese, garbled problems may occur the //Solution: 1.native2ascii.exe convert Chinese to Unicode encoding; 2. Package the InputStream into reader the //InputStream byte stream cannot read Chinese, convert InputStream into reader character stream, decode bytes to characters to read Chinese theInputStreamReader ISR =NULL; - Try { inISR =NewInputStreamReader (Is3, "UTF-8"); the}Catch(unsupportedencodingexception e) { the e.printstacktrace (); About } the //where UTF-8, the character set used to explicitly specify the decoded character stream the //without specifying the OS is used by default, which results in the same copy of the same profile, inconsistent between Linux and Windows, English windows, and Chinese Windows the //This parameter should match the encoding format of the properties file for the specific read + - the //It is recommended to use BufferedReader to wrap all of their read () operations with potentially expensive Reader (such as FileReader and InputStreamReader)Bayi //if not buffered, the file will be accessed again and again through the Read method theBufferedReader BF =NewBufferedReader (ISR); the - //read the content into the Properties - Try { the prop.load (BF); the}Catch(IOException e) { the //TODO auto-generated Catch block the e.printstacktrace (); - } the theString user = Prop.getproperty ("name"); the System.out.println (user);94 the //get the Name of all the property theSet<string> set =prop.stringpropertynames (); theIterator<string> it =set.iterator ();98 while(It.hasnext ()) { AboutString name =It.next (); -String value = prop.getproperty (name);//get value by name101SYSTEM.OUT.PRINTLN (name + ":" +value);102 }103 }104}
Java Read Properties configuration file