码迷,mamicode.com
首页 > 编程语言 > 详细

java配置文件的读写

时间:2016-12-07 01:26:15      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:res   记录   loader   put   获取   code   store   length   ade   

最近在做一个爬虫项目时,用到了读写配置文件的方法,记录下来以后可能用的到。

Properties pro = new Properties();
boolean IsFirst = true;
//从类加载路径读取文件,所以文件位置需要放对
InputStream in = PropertiesUtil.class.getClassLoader()
                .getResourceAsStream("config.properties");
pro.load(in);
//获取properties属性       
IsFirst = Boolean.parseBoolean((String) pro.get("isFirstStart"));
in.close();

//获取properties所在的路径
String sourceUrl = this.getClass().getClassLoader()
                .getResource("config.properties").toString();
//截取其中一部分,(不懂的实验下就知道了) sourceUrl
= sourceUrl.substring(6, sourceUrl.length()); pro.setProperty("isFirstStart", "false");
FileOutputStream out
= new FileOutputStream(sourceUrl);
//将修改后的存储 pro.store(out,
"is First start"); out.close();
isFirstStart=true //config.properties文件内容

 

java配置文件的读写

标签:res   记录   loader   put   获取   code   store   length   ade   

原文地址:http://www.cnblogs.com/huangyichun/p/6139533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!