conf.properties文件内容:
reportStationName=xx供电局
JBM=0318

文件路径:

六种方式读取properties资源文件_bundle


其中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文件读取工具

*/
public class LoadPropertiesFileUtil {

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"));
}

}

后台打印结果:
getPropertie1=>>>>>>0318
getPropertie2
=>>>>>>0318
getPropertie3=>>>>>>0318
getPropertie4
=>>>>>>0318
getPropertie5=>>>>>>0318
getPropertie6
=>>>>>>0318