浣嗘槸鍦ㄦ湇鍔″櫒榪愯榪囩▼涓?鏃犺鎬庝箞鏇存敼test.properties
寰楀嚭鐨勬暟鎹繕鏄渶鍒濈殑閭d釜
鎴戝悗鏉ヨ瘯浜嗕竴涓?浠g爜濡備笅
package test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test {
Properties pp = null;
public Properties getData() throws IOException {
InputStream is = getClass().getResourceAsStream("/test.properties");
// InputStream is = new FileInputStream(
// "D:\\java\\apache-tomcat-5.5.17\\apache-tomcat-5.5.17\\webapps\\testp\\WEB-INF\\classes\\test.properties");
System.out.println(is.hashCode());
pp = new Properties();
pp.load(is);
System.out.println(pp.hashCode());
// Properties pp = System.getProperties();
// Enumeration<String> enu = (Enumeration<String>) pp.propertyNames();
// while(enu.hasMoreElements()){
// String name = enu.nextElement();
// System.out.println(name + "=" +pp.getProperty(name));
// }
// is.close();
is.close();
return pp;
}
public static Properties getProperties() {
try {
return new Test().getData();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
while (true) {
System.out.println(getProperties());
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
涓婇潰榪欎釜瑙f瀽鐨凱roperties pp 鐨刪ashcode 濮嬬粓涓嶅彉
鑰孖nputStream 鐨刪ash 緙轟竴鐩存敼鍙?br>
鐢辨鍙互鎺ㄦ柇緇撹鏈?br>getClass().getResourceAsStream() 鏄疌lassLoader 鍔犺澆Class涓鏍風殑鎶妕est.properties 鍔犺澆榪涗簡鍐呭瓨
浣嗘槸閽堝涓婇潰鐨勭孩瀛楁垜鍐欎簡MAIN鍑芥暟鍋氫負嫻嬭瘯
鐜板湪鍙戠幇濡傛灉鎴戞洿鏀筆roperties 錛屼細绔嬪埢鍋氬嚭鍙嶅簲
main鍑芥暟涓殑浠g爜鎴戠殑鐞嗚В濡備笅 姣忔ClassLoader閮藉湪鍔犺澆ClassPath涓嬬殑鏂囦歡錛屽綋鍙戠幇鏀瑰彉灝辨瀯鎴怭roperties 鏀瑰彉
鎵浠ユ垜鎯寵繖鏄笉鏄痶omcat鐨凜lassLoader 鐨勪竴涓猙ug ??
浠ヤ笂璦璁猴紝鍧囧睘鎴戠殑鐚滄祴錛岃繕鏈涢珮鎵嬫寚鐐?
鏂板彂鐜?
InputStream is = Test.class.getClassLoader().getResourceAsStream(// .getResourceAsStream(
InputStream is = Test.class.getResourceAsStream(// .getResourceAsStream(
榪欐牱鍔犺澆鐨凱roperties 鏄袱涓笉鍚岀殑瀹炰緥 鎴戠殑鎰忔濇槸鎯寵涓や喚涓嶅悓鐨勫唴瀛?br>
鎵浠ュ鏋滄兂test.properties闅忔椂鍙?鐢ㄧ粷瀵硅礬寰?InputStream is = new FileInputStream("緇濆璺緞")
灝卞彲浠ヤ簡

]]>