Read the configuration file under SRC in the code
Read Log4j.properties and message.properties under the SRC path
Read the Message.properties file and convert the key-value pairs in the properties to map
PropertiesServlet.class.getClassLoader (). getResourceAsStream ("/message.properties"); The return value is a InputStream
/** * reads information according to the Java standard properties file and assigns a value of hashmap<string, string> * @param path * @param map * @return * @throws Exception */ public final map<string, String> filetomap (Map<string,string> map) throws Exception{ if (map == null) { map = new HashMap<String,String> (); } InputStream isr = null; Reader r = null; try { &nBsp; isr = propertiesservlet.class.getclassloader (). getResourceAsStream ("/message.properties"); r = new inputstreamreader (isr, "UTF-8"); properties props = new properties ();//use: properties props.load (R); Set<Entry<Object, Object>> Entryset = props.entryset (); for (Entry<object, object> entry : entryset) { if (!entry.getkey (). toString (). StartsWith ("#")) { map.put ((String) Entry.getkey ()). Trim (), ((String) entry . GetValue ()). Trim ()); } } return map; } finally { if (r != null) { try { R.close (); } catch (ioexception e) { hlogger.error (e); } } if (Isr != null) { try { isr.closE (); } catch (EXCEPTION&NBSP;E2) { hlogger.error (E2); } } } }
LogInit.class.getClassLoader (). GetResource ("//"). GetPath (); Returns a path of type string
public void Init () throws Servletexception {//TODO auto-generated Method Stub super.init (); Propertyconfigurator.configure (LogInit.class.getClassLoader (). GetResource ("//"). GetPath () + "// Log4j-hrp.properties "); }
Common methods for obtaining files under SRC paths in Java