首页 > 其他 > 详细

方法总结

时间:2015-06-05 19:04:04      阅读:211      评论:0      收藏:0      [点我收藏+]

1. 加载配置文件

public static String getConfig(String fileName) {

        StringBuilder sb = new StringBuilder();
        
        InputStream in = null;
        try {
            
            // 获取当前jar包所在路径
            String path = ConfigLoader.class.getProtectionDomain().getCodeSource().getLocation().toString();
            int begin = path.indexOf(":")+1;
            int end = path.lastIndexOf("/")+1;
            String userDir = path.substring(begin,end);
            
            File confInDir = new File(userDir + File.separator + fileName);
            URL confInClassPath = ConfigLoader.class.getResource("/" + fileName);
            
// 普通配置文件获取
if(confInDir.exists()){ System.out.println("Load Config File:"+confInDir); in = new FileInputStream(confInDir); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); String line = null; while((line=reader.readLine())!=null){ sb.append(line).append(‘\n‘); } reader.close(); }else if(null != confInClassPath){
// web 服务配置文件获取class目录下 in
= ConfigLoader.class.getResourceAsStream("/" + fileName); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); String line = null; while((line=reader.readLine())!=null){ sb.append(line).append(‘\n‘); } reader.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if(null != in){ try { in.close(); } catch (IOException e) { } } } return sb.toString(); }

 

方法总结

原文:http://www.cnblogs.com/Jtianlin/p/4555068.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!