獲取配置檔案資料
阿新 • • 發佈:2019-01-01
package ; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.chinainsurance.sysframework.exception.BusinessException; public class PathUtils{ public static String getPath(String pathId){ Properties props = new Properties(); InputStream is =PathUtils.class.getClassLoader().getResourceAsStream("Common.properties"); try { if(is==null){ throw new Exception("配置檔案不存在"); } props.load(is); } catch (Exception e) { e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { } } } String vaule =props.getProperty(pathId); return vaule; } /** * 讀取本機IP * @return * @throws Exception */ public static String getLocalHost() throws Exception { Properties prop = new Properties(); String localHost = ""; InputStream in = null; try{ //讀取屬性檔案nativeIp.properties in = new BufferedInputStream (new FileInputStream(System.getProperty("user.dir")+"/businessConfig/system.properties")); prop.load(in); ///載入屬性列表 localHost = prop.getProperty("nativeip"); in.close(); }catch(Exception e){ e.printStackTrace(); throw new BusinessException("讀取本地IP異常,請處理!",null); }finally{ in.close(); } return localHost; } public static void main(String[] args) { System.out.println(PathUtils.getPath("app1")); } }