讀取jar包內配置文件


public class Config {

  private static Properties properties;
  private static Logger logger = Logger.getLogger(Config.class);
  static {
   try {
    PropertyConfigurator.configure(System.getProperty("user.dir")
      + "/config/log4j.properties");
    properties = new Properties();
    // 讀取SRC下配置文件 --- 屬於讀取內部文件
    // properties.load(Config.class.getResourceAsStream("/init.properties"));
    // 讀取系統外配置文件 (即Jar包外文件) --- 外部工程引用該Jar包時需要在工程下創建config目錄存放配置文件
    String filePath = System.getProperty("user.dir")
    + "/config/init.properties";
    InputStream in = new BufferedInputStream(new FileInputStream(filePath));
    properties.load(in);
   } catch (IOException e) {
             logger.error("讀取配置信息出錯!", e);
   }
  }
  public static String getObject(String prepKey) {
            return properties.getProperty(prepKey);
  }

  public static void main(String[] agrs) {
           logger.info(Config.getObject("testsql"));
  }
 }

System.getProperty("user.dir") 獲得項目的絕對路徑,然后拼裝配置文件的路徑。

  


免責聲明!

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



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