Common methods for obtaining files under SRC paths in Java

Source: Internet
Author: User

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

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.