main方法怎么获取配置文件中的值
1、创建个配置文件,配置中写你要配置的参数。如下图
2、然后建个main方法
方法一:public static void main(String[] args) throws UnsupportedEncodingException {
ResourceBundle resource = ResourceBundle.getBundle("application");
String value = new String(resource.getString("ip").trim().getBytes("ISO-8859-1"), "UTF8");//防止中文乱码
}
3、方法二:
InputStream in = getClass().getResourceAsStream("application");
这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:InputStream ins = this.getClass().getResourceAsStream("/xxxx/src/main/resources/application.properties");
Properties prop = new Properties();
prop.load(in);String ip = prop.getProperty( "ip" ).trim();
4、这样就读取到我们配置的ip的值了。
5、个人更喜欢用第二种常见的方法去读取。希望帮到你们的时候别忘了给我点赞投票哈哈哈,不然都没有什么让我坚持下去继续写经验了。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:67
阅读量:53
阅读量:84
阅读量:84
阅读量:84