標籤:fileinput icon this ring 代碼 strong bar config source
一:如果容器為Websphere,那下面為紅色的地方不能加"/",如果為tomcat,則加上"/",
Java代碼
- String path = this.class.getResource("").getPath()+"config.properties";
-
- Properties properties= new Properties();
-
- properties.load(new FileInputStream(new File(path )));
如果你的config.properties在某個包下面,則把包同時帶上,如:config.properties在com.df.util包下,則為:
Java代碼
- String path = this.class.getResource("").getPath()+"com/df/util/config.properties";
-
- Properties properties= new Properties();
-
- properties.load(new FileInputStream(new File(path )));
二:如果你的項目中用到了spring,那麼也可這樣擷取,
Java代碼
- import org.springframework.core.io.Resource;
-
- import org.springframework.core.io.ClassPathResource;
-
- Resource resource = new ClassPathResource("config.properties"); //直接讀取src下的,位於class檔案之下
-
- Properties properties= new Properties();
-
- InputStream in = resource.getInputStream();
-
- properties.load(in);
Websphere中擷取項目下.properties路徑