Six ways to summarize Java loading properties files

Source: Internet
Author: User

Six ways to summarize Java loading properties files

Java Load properties file Six basic ways to implement

The way Java loads the properties file is divided into two major categories:

"One is loaded by the load (InputStream in) method in the Import Java.util.Properties class;

The other is loaded by the Getbundle (String baseName) method of the Import Java.util.ResourceBundle class.

Note: Be sure to differentiate the path format

The implementation code is as follows:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131 package com.util;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.PropertyResourceBundle;import java.util.ResourceBundle; public class PropertiesUtil {  private static String basePath = "src/prop.properties";  private static String name = "";  private static String nickname = "";  private static String password = "";  /**   * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件   *    */  public static String getName1() {    try {      Properties prop = new Properties();      InputStream is = new FileInputStream(basePath);      prop.load(is);      name = prop.getProperty("username");    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return name;  }  /**   * 二、 使用class变量的getResourceAsStream()方法   * 注意:getResourceAsStream()读取路径是与本类的同一包下   *    */  public static String getName2() {    Properties prop = new Properties();    InputStream is = PropertiesUtil.class        .getResourceAsStream("/com/util/prop.properties");    try {      prop.load(is);      name = prop.getProperty("username");    } catch (IOException e) {      e.printStackTrace();    }    return name;  }  /**   * 三、   * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法   * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀 否则会报空指针异常   *    */  public static String getName3() {    Properties prop = new Properties();    InputStream is = PropertiesUtil.class.getClassLoader()        .getResourceAsStream("com/util/prop.properties");    try {      prop.load(is);    } catch (IOException e) {      e.printStackTrace();    }    return name;  }  /**   * 四、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法   * getSystemResourceAsStream()方法的参数格式也是有固定要求的   *    */  public static String getName4() {    Properties prop = new Properties();    InputStream is = ClassLoader        .getSystemResourceAsStream("com/util/prop.properties");    try {      prop.load(is);      name = prop.getProperty("username");    } catch (IOException e) {      e.printStackTrace();    }    return name;  }  /**   * 五、 使用java.util.ResourceBundle类的getBundle()方法   * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常   *    */  public static String getName5() {    ResourceBundle rb = ResourceBundle.getBundle("com/util/prop");    password = rb.getString("password");    return password;  }  /**   * 六、 使用java.util.PropertyResourceBundle类的构造函数   *    */  public static String getName6() {    try {      InputStream is = new FileInputStream(basePath);      ResourceBundle rb = new PropertyResourceBundle(is);      nickname = rb.getString("nickname");    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return nickname;  }  /**   * 测试   *    */  public static void main(String[] args) {    System.out.println("name1:" + PropertiesUtil.getName1());    System.out.println("name2:" + PropertiesUtil.getName2());    System.out.println("name3:" + PropertiesUtil.getName3());    System.out.println("name4:" + PropertiesUtil.getName4());    System.out.println("password:" + PropertiesUtil.getName5());    System.out.println("nickname:" + PropertiesUtil.getName6());  }}

Six ways to summarize Java loading properties files

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.