天天看點

六種方式讀取properties資源檔案

conf.properties檔案内容:

reportStationName=xx供電局

JBM=0318

檔案路徑:

六種方式讀取properties資源檔案

其中xxx為項目名

import java.io.BufferedInputStream;

import java.io.File;

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;

/**

  • @Author 你的辣條分我點
  • @Date 2019-4-19 下午4:04:04
  • @Version 1.0 業務說明:properties檔案讀取工具
private static String webPath = System.getProperty("user.dir");// E:\Program Files (x86)\workspace\oms
private static String fileName = "config.properties";

/**
 * 一、 使用java.util.Properties類的load(InputStream in)方法加載properties檔案
 * @return
 */
public static Properties getPropertie1(String basePath) {

  Properties prop = new Properties();
  try {
    InputStream in = new BufferedInputStream(new FileInputStream(new File(basePath)));
    prop.load(in);

  } catch (FileNotFoundException e) {
    System.out.println("properties檔案路徑書寫有誤,請檢查!");
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }

  return prop;
}

/**
 * 二、 使用java.util.ResourceBundle類的getBundle()方法
 * 注意:這個getBundle()方法的參數隻能寫成包路徑+properties檔案名,否則将抛異常
 * 不需要檔案字尾名,如果在src下面,直接寫檔案名即可(包路徑不用寫)
 * @return
 */
public static ResourceBundle getPropertie2(String basePath) {
  ResourceBundle rb = ResourceBundle.getBundle(basePath);
  return rb;
}

/**
 * 三、 使用java.util.PropertyResourceBundle類的構造函數(傳入檔案流InputStream)
 * @return
 */
public static ResourceBundle getPropertie3(String basePath) {
  InputStream in;
  ResourceBundle rb = null;
  try {
    in = new BufferedInputStream(new FileInputStream(basePath));
    rb = new PropertyResourceBundle(in);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return rb;
}

/**
 * 四、 使用class變量的getResourceAsStream()方法
 * 注意:getResourceAsStream()方法的參數按格式寫到包路徑+properties檔案名+.字尾
 * 如果檔案在跟目錄下,直接寫檔案.字尾名
 * @return
 */
public static Properties getPropertie4(String basePath) {
  InputStream in = LoadPropertiesFileUtil.class.getResourceAsStream(basePath);
  Properties p = new Properties();
  try {
    p.load(in);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return p;
}

/**
 * 五、
 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
 * getResourceAsStream(name)方法的參數必須是包路徑+檔案名+.字尾 否則會報空指針異常
 */
public static Properties getPropertie5(String basePath) {
  InputStream in = LoadPropertiesFileUtil.class.getClassLoader().getResourceAsStream(basePath);
  Properties p = new Properties();
  try {
    p.load(in);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return p;
}

/**
 * 六、 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜态方法
 * getSystemResourceAsStream()方法的參數格式也是有固定要求的
 */
public static Properties getPropertie6(String basePath) {
  InputStream in = ClassLoader.getSystemResourceAsStream(basePath);
  Properties p = new Properties();
  try {
    p.load(in);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return p;
}

public static void main(String[] args) {
  String basePath = webPath + File.separator + "resources" + File.separator + fileName;
  System.out.println("getPropertie1===>>>>>>" + LoadPropertiesFileUtil.getPropertie1(basePath).getProperty("JBM"));
  fileName = fileName.substring(0, fileName.lastIndexOf("."));
  // System.out.println(fileName);//config
  System.out.println("getPropertie2===>>>>>>" + LoadPropertiesFileUtil.getPropertie2(fileName).getString("JBM"));
  System.out.println("getPropertie3===>>>>>>" + LoadPropertiesFileUtil.getPropertie3(basePath).getString("JBM"));
  fileName = "config.properties";
  System.out.println("getPropertie4===>>>>>>" + LoadPropertiesFileUtil.getPropertie4(fileName).getProperty("JBM"));
  System.out.println("getPropertie5===>>>>>>"+LoadPropertiesFileUtil.getPropertie5(fileName).getProperty("JBM"));
  System.out.println("getPropertie6===>>>>>>"+LoadPropertiesFileUtil.getPropertie6(fileName).getProperty("JBM"));
}