我正在使用servlet,jsp和java類構建Web應用程序。屬性文件無法從Java類訪問
。由此java的結構
我想作爲文件存儲項目配置訪問system.properties
文件。
我做什麼用FileInputStream
使用這個Java代碼
/* generate the properties file objects */
Properties prop = new Properties();
/* generate IO to read from properties file */
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties");
if (inputStream != null) {
/* load the properties file */
prop.load(inputStream);
的把所有的變量從文件轉換成目標越來越文件
host = prop.getProperty("host");
但它顯示錯誤nullpointerexception
我知道屬性文件中的參數未加載。
Apr 26, 2015 7:21:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.domain.servlet.TotalErrorSummaryServlet] in context with path [/MonitoringDashboard] threw exception
java.lang.NullPointerException
任何建議編輯system.properties
文件的加載程序?當我把文件放在WEB-INF文件夾中時。
把'system.properties'放入'WEB-INF/classes'中,並在' getResourceAsStream(「/ system.properties」)' – morgano
嘗試'InputStream inputStream = class.getClass()。getC lassLoader()。getResourceAsStream(「WEB-INF/system.properties」);'而不是你的'inputStream'。 –
如果屬性文件不可訪問,那麼當您執行'prop.load()'時,您會在getProperty之後記錄主機的值嗎? – RaGe