歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 將properties文件放在Jar包並讀取

將properties文件放在Jar包並讀取

日期:2017/3/1 10:18:36   编辑:Linux編程

有時候需要在一個library內部打包一個properties文件,包含一些配置信息,而不能部署在外部。

在maven工程裡面,將properties文件放在src/main/resources目錄下。就會自動打包到classes目錄下。

然後在jar包的代碼中想要讀取這個文件,可以用類似下面的代碼:

  1. public class Client {
  2. private final static String NAME = "engine.properties";
  3. public static Map<String, String> load() throws AppEngineException {
  4. InputStream stream = Client.class.getClassLoader().getResourceAsStream(NAME);
  5. ...
  6. }
  7. }

一切OK.

Copyright © Linux教程網 All Rights Reserved