读写Poperties文件

编程技术  /  houtizong 发布于 3年前   71
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.util.Properties;import com.jeecms.download.entity.LilvEntity;public class LilvManageDao {public LilvEntity queryLilv() throws IOException {ClassLoader classLoader = Thread.currentThread().getContextClassLoader();if (classLoader == null) {classLoader = this.getClass().getClassLoader();}URL inputUrl = classLoader.getResource("a.properties");File file = new File(inputUrl.getPath());InputStream in = new BufferedInputStream(new FileInputStream(inputUrl.getPath()));Properties p = new Properties();p.load(in);LilvEntity li = new LilvEntity();li.setGjj_1_3(p.getProperty("gjj_1_3"));li.setGjj_3_5(p.getProperty("gjj_3_5"));li.setGjj_5__(p.getProperty("gjj_5__"));li.setDk_1_3(p.getProperty("dk_1_3"));li.setDk_3_5(p.getProperty("dk_3_5"));li.setDk_5__(p.getProperty("dk_5__"));li.setCalc_num(p.getProperty("calc_num"));in.close();return li;}/** * 更新properties文件的键值对 如果该主键已经存在,更新该主键的值;如果该主键不存在,则插件一对键值。 *  * @param keyname *            键名 *@param keyvalue *            键值 */public void updateProperties(String keyname, String keyvalue) {ClassLoader classLoader = Thread.currentThread().getContextClassLoader();if (classLoader == null) {classLoader = this.getClass().getClassLoader();}URL inputUrl = classLoader.getResource("a.properties");File file = new File(inputUrl.getPath());try {Properties props = new Properties();props.load(new FileInputStream(inputUrl.getPath()));// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。OutputStream fos = new FileOutputStream(inputUrl.getPath());props.setProperty(keyname, keyvalue);// 以适合使用 load 方法加载到 Properties 表中的格式,// 将此 Properties 表中的属性列表(键和元素对)写入输出流props.store(fos, "Update '" + keyname + "' value");fos.flush();} catch (IOException e) {System.err.println("属性文件更新错误");}}}

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客