1、基於ClassLoder讀取配置文件
注意:該方式只能讀取類路徑下的配置文件,有局限但是如果配置文件在類路徑下比較方便。
InputStream inputStream1 = PropertiesTest.class.getResourceAsStream("filepath");
2、基於 InputStream 讀取配置文件
注意:該方式的優點在於可以讀取任意路徑下的配置文件。
InputStream inputStream1 = new BufferedInputStream(new FileInputStream("filepath"));
3、舉例:
(1)結構:

(2)代碼
public class PropertiesUtil { public static final PropertiesUtil INSTANCE = new PropertiesUtil(); public static PropertiesUtil getInstance(){ return INSTANCE; } /** * 1.使用getResourceAsStream讀取配置文件 */ public void readPropertiesByGetSourceAsStream(){ // 1.src下面com/demo/config包內的配置文件config1.properties InputStream inputStream1 = PropertiesTest.class.getClassLoader().getResourceAsStream("com/demo/config/config1.properties"); // 2.讀取和PropertiesTest位於同一個包內的配置文件config2.properties InputStream inputStream2 = PropertiesTest.class.getResourceAsStream("config2.properties"); // 3.讀取src根目錄下的配置文件config3.properties InputStream inputStream3 = PropertiesTest.class.getClassLoader().getResourceAsStream("config3.properties"); // 4.讀取位於另外一個resource文件夾里面的配置文件config4.properties InputStream inputStream4 = PropertiesTest.class.getClassLoader().getResourceAsStream("config4.properties"); Properties properties = new Properties(); System.out.println("使用getResourceAsStream()配置文件..."); try { System.out.println("-----讀取inputStream1開始-----"); properties.load(inputStream1); System.out.println("config1.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("url")); System.out.println("-----讀取inputStream1結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream2開始-----"); properties.load(inputStream2); System.out.println("config2.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("url")); System.out.println("-----讀取inputStream2結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream3開始-----"); properties.load(inputStream3); System.out.println("jdbc.properties:"); // 這里的%s是java String占位符 System.out.println(String.format("jdbc.driver=%s",properties.getProperty("jdbc.driver"))); System.out.println(String.format("jdbc.url=%s",properties.getProperty("jdbc.url"))); System.out.println(String.format("jdbc.username=%s",properties.getProperty("jdbc.username"))); System.out.println(String.format("jdbc.password=%s",properties.getProperty("jdbc.password"))); System.out.println("-----讀取inputStream3結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream4開始-----"); properties.load(inputStream4); System.out.println("config.properties:"); System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser"))); System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword"))); System.out.println(MessageFormat.format("database={0}", properties.getProperty("database"))); System.out.println("-----讀取inputStream4結束-----"); System.out.println("---------------------------------------------"); } catch (IOException e) { e.printStackTrace(); }finally { if(null != inputStream1){ try { inputStream1.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != inputStream2){ try { inputStream2.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != inputStream3){ try { inputStream3.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != inputStream4){ try { inputStream4.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 2.使用InputStream讀取配置文件 */ public void readPropertiesByInputStream(){ InputStream inputStream1 = null; InputStream inputStream2 = null; InputStream inputStream3 = null; InputStream inputStream4 = null; System.out.println("使用InputStream讀取配置文件..."); try { // 1.src下面com/demo/config包內的配置文件config1.properties inputStream1 = new BufferedInputStream(new FileInputStream("src/com/demo/config/config1.properties")); // 2.讀取和PropertiesTest位於同一個包內的配置文件config2.properties inputStream2 = new BufferedInputStream(new FileInputStream("src/com/demo/day8/config2.properties")); // 3.讀取src根目錄下的配置文件config3.properties inputStream3 = new BufferedInputStream(new FileInputStream("src/config3.properties")); // 4.讀取位於另外一個resource文件夾里面的配置文件config4.properties inputStream4 = new BufferedInputStream(new FileInputStream("resource/config4.properties")); Properties properties = new Properties(); try { System.out.println("-----讀取inputStream1開始-----"); properties.load(inputStream1); System.out.println("config1.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("url")); System.out.println("-----讀取inputStream1結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream2開始-----"); properties.load(inputStream2); System.out.println("config2.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("url")); System.out.println("-----讀取inputStream2結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream3開始-----"); properties.load(inputStream3); System.out.println("jdbc.properties:"); // 這里的%s是java String占位符 System.out.println(String.format("jdbc.driver=%s", properties.getProperty("jdbc.driver"))); System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url"))); System.out.println(String.format("jdbc.username=%s", properties.getProperty("jdbc.username"))); System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password"))); System.out.println("-----讀取inputStream3結束-----"); System.out.println("---------------------------------------------"); System.out.println("-----讀取inputStream4開始-----"); properties.load(inputStream4); System.out.println("config.properties:"); System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser"))); System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword"))); System.out.println(MessageFormat.format("database={0}", properties.getProperty("database"))); System.out.println("-----讀取inputStream4結束-----"); System.out.println("---------------------------------------------"); } catch (IOException e) { e.printStackTrace(); } finally { if (null != inputStream1) { try { inputStream1.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream2) { try { inputStream2.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream3) { try { inputStream3.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream4) { try { inputStream4.close(); } catch (IOException e) { e.printStackTrace(); } } } }catch (FileNotFoundException e) { e.printStackTrace(); } } } 測試: public class PropertiesTest { /** * 1.使用getSourceAsStream讀取配置文件測試 */ @Test public void readPropertiesByGetSourceAsStreamTest(){ PropertiesUtil.getInstance().readPropertiesByGetSourceAsStream(); } /** * 2.使用InputStream讀取配置文件測試 */ @Test public void readPropertiesByInputStreamTest(){ PropertiesUtil.getInstance().readPropertiesByInputStream(); } }
測試結果:

config4.properties不在測試類的路徑下,不能通過ClassLoder的方式讀取配置文件。

InputStream可以讀取任何路徑下的配置文件。
(4)讀取配置文件中的全部
以讀取config3.properties為例; public void readAllInfo() { /* * 讀取src根目錄下面的配置文件 * config3.properties */ InputStream is3 = PropertiesTest.class.getClassLoader().getResourceAsStream("config3.properties"); Properties p = new Properties(); try { p.load(is3); //讀取資源文件的所有的key值(列舉) Enumeration en= p.propertyNames(); System.out.println("讀取is3開始..."); while(en.hasMoreElements()) { String key = (String) en.nextElement(); System.out.println(key + "=" + p.getProperty(key)); } System.out.println("讀取is3結束..."); } catch (IOException e) { e.printStackTrace(); System.out.println("讀取資源文件出錯"); } finally { if(null != is3) { try { is3.close(); System.out.println("關閉is3..."); } catch (IOException e) { e.printStackTrace(); } } } }
測試結果:

參考:https://www.cnblogs.com/sebastian-tyd/p/7895182.html
測試結果:
[Cèshì jiéguǒ:]
Test Results:
