六種方式讀取properties資源文件


conf.properties文件內容:
reportStationName=xx供電局
JBM=0318

文件路徑:


其中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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM