您当前的位置: 首页 >  Java

zmc@

暂无认证

  • 0浏览

    0关注

    142博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java读配置文件

zmc@ 发布时间:2018-08-20 12:40:44 ,浏览量:0

读取配置文件的方案:

public String getProperty(String name,String k){
            Properties prop = new Properties();
            String key=null;
            String val=null;
            try{
                //读取属性文件a.properties
                InputStream in = new BufferedInputStream (new FileInputStream(name));
                prop.load(in);     ///加载属性列表
                Iterator it=prop.stringPropertyNames().iterator();
                while(it.hasNext()){
                    key=it.next();
                    if(key.equals(k)){
                        System.out.println(key+":"+prop.getProperty(key));
                        val=prop.getProperty(key);
                        break;
                    }
                 }
                in.close();
            }catch(Exception e){
                System.out.println(e);
            }
            return val;
        }

在springboot中,可以将其设置为aop的切面;

 

注意:第一个参数name,直接写a.properties,只能读取到该目录下的配置文件;

若在其他目录中,则:

method.getProperty("src\\main\\resources\\a.properties" ,"localPath");
 
关注
打赏
1643271353
查看更多评论
立即登录/注册

微信扫码登录

0.0909s