java讀取properties配置文件

jopen 8年前發布 | 26K 次閱讀 Java開發

Properties properties =new Properties();

// CASE 1 try{     //在加載的class文件中加載,文件是和類文件放在一下的     ClassLoader loader =PropertiesUtil.class.getClassLoader();     InputStream inStream = loader.getResourceAsStream("config.properties");     properties.load(inStream);     value = properties.getProperty(propKey); }catch(Exception e){     logger.error("An error occured!"); }

// CASE 2 try{     //loadAllProperties直接使用路徑     properties = PropertiesLoaderUtils.loadAllProperties("E:/config.properties");     properties.getProperty(propKey); }catch(Exception e){     logger.error("An error occured!"); }

// CASE 3 try{     Resource resource = new ClassPathResource("config.properties");     properties =PropertiesLoaderUtils.loadProperties(resource);     value = properties.getProperty(propKey); }catch(Exception e){     logger.error("An error occured!"); }

// CASE 4 try{     Resource resource = new ClassPathResource("config.properties");     PropertiesLoaderUtils.fillProperties(properties, resource);     value = properties.getProperty(propKey); }catch(Exception e){     logger.error("An error occured!"); }</pre>


來自: http://my.oschina.net/chenhao901007/blog/472465

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!