1. 程式人生 > >java獲取tomcat中的properties檔案

java獲取tomcat中的properties檔案

System.getProperty("catalina.home")

獲取tomcat的絕對路徑

獲取檔案的絕對路徑

在windous中拼接路徑是" \ " 而linux系統中路徑拼接是" / " 所以為了更好的相容建議用File.separator

例子如下:

properties檔案的路徑及內容

 

java程式碼

private static String getRestfulAddress() {
        Properties pps = new Properties();
        try {
            String dir2 
= System.getProperty("catalina.home") + File.separator +"lib" + File.separator + "emc" + File.separator + "test.properties"; FileInputStream fs = new FileInputStream(dir2); InputStream in = new BufferedInputStream (fs); pps.load(in); String restfulAddress
= pps.getProperty("restfulAddress"); in.close(); return restfulAddress; } catch (Exception e) { e.printStackTrace(); } return null; }