java jar運行外部配置文件(.properties)

解決方法如下:

1.通常導入項目內的配置文件(.properties文件)是如下代碼:

     Properties properties = new Properties();
    // 使用ClassLoader加載properties配置文件生成對應的輸入流
    InputStream in = Propertiesxxx.class.getClassLoader().getResourceAsStream("xxx.properties");
    // 使用properties對象加載輸入流
    properties.load(in);
    //獲取key對應的value值
    properties.getProperty(String key)

其中Propertiesxxx項目類名稱

2.用這種辦法讀取配置文件(.properties文件)時,當你將項目導出成jar包時,是無法讀取外部的配置文件(.properties文件),所以我們將該成讀取外部的配置文件(.properties文件),代碼如下:

Properties properties = new Properties();
FileInputStream in= new FileInputStream("xxx.properties");
properties.load(in);
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章