1. 程式人生 > >將properties檔案放在Jar包並讀取

將properties檔案放在Jar包並讀取

有時候需要在一個library內部打包一個properties檔案,包含一些配置資訊,而不能部署在外部。

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

然後在jar包的程式碼中想要讀取這個檔案,可以用類似下面的程式碼:

public class Client {

    private final static String NAME = "engine.properties";

    public static Map<String, String> load() throws AppEngineException {
	InputStream stream = Client.class.getClassLoader().getResourceAsStream(NAME);
	...
    }
}


一切OK.