Three methods for loading configuration files in java

Source: Internet
Author: User

For example, we need to load the db. properties file under the source directory. There are the following methods: file io stream: copy the code public static void load1 () throws Exception {// true file path String fileName = "E: /Workspace/SSHDemo/Source/db. properties "; Properties p = new Properties (); InputStream is = new FileInputStream (new File (fileName); p. load (is); System. out. println (p);} copy the second type of code: Relative Path: copy the code // relative path public static void load2 () throws Exception {Properties p = new Properties (); // InputStream is = ClassLoader. getSyst EmResourceAsStream ("db. properties "); InputStream is = Thread. currentThread (). getContextClassLoader (). getSystemResourceAsStream ("db. properties "); p. load (is); System. out. println (p);} public static void load2_1 () throws Exception {Properties p = new Properties (); InputStream is = SourceLoader. class. getClassLoader (). getSystemResourceAsStream ("db. properties "); p. load (is); System. out. println (p);} copy the Code if we want to obtain What should I do with db. properties under src (class package? Copy the code // put java together with the properties file box of the class path public static void load3 () throws Exception {Properties p = new Properties (); // InputStream is = ClassLoader. getSystemResourceAsStream ("db. properties "); InputStream is = SourceLoader. class. getResourceAsStream ("db. properties "); p. load (is); System. out. println (p );

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.