java 读取配置文件 与更新
笔记
public class Config {
private static Properties props = new Properties();
static File configFile = null;
static {
InputStreamReader reader = null;
try {
File dir = new File(System.getProperty("user.dir"));
configFile = new File(dir, "config.properties.dev");
if (!configFile.exists()) {
// String path = Thread.currentThread().getClass().getClassLoader().getResource("config.properties").getPath();
// configFile = new File(path);
configFile = new File(dir, "config.properties");
}
reader = new InputStreamReader(new FileInputStream(configFile), "UTF-8");
props.load(reader);
} catch (FileNotFoundException e) {
} catch (Exception e) {
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
public static String getDsl(){
System.out.println("dsl" + props.getProperty("dsl"));
return props.getProperty("dsl");
}
public static String getDate(){
System.out.println("date" + props.getProperty("date"));
return props.getProperty("date");
}
public static void updateProperties(String keyname,String keyvalue) {
try {
props.load(new FileInputStream(configFile));
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
props.setProperty(keyname, keyvalue);
OutputStream fos = new FileOutputStream(configFile);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
}
java 读取配置文件 与更新的更多相关文章
- java读取配置文件的几种方法
java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...
- Java读取配置文件的方式
Java读取配置文件的方式-笔记 1 取当前启动文件夹下的配置文件 一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...
- java读取配置文件(转)
转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件 方式一:采用Servle ...
- java读取配置文件
java 读取文件可以用字节流和字符流. 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码. 用字符流则不会出现乱码. 配置文件 b.properties 文件如下: fam ...
- Java 读取配置文件数据
Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...
- java读取配置文件方法以及工具类
第一种方式 : java工具类读取配置文件工具类 只是案例代码 抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...
- java读取配置文件内容
利用com.typesafe.config包实现 <dependency> <groupId>com.typesafe</groupId> <artifact ...
- spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入
一.配置注解读取配置文件 (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值 实例: @PropertySource(val ...
- 使用Java读取配置文件
实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOExcep ...
随机推荐
- c# 后台线程 访问前台控件并显示信息
//设置为后台线程 Thread th = new Thread(delegate() { append(); }); th.IsBackground = true; th.Start(); //在a ...
- 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)
洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...
- CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)
传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...
- Python函数中参数类型
在学习Python函数的时候,函数本身的定义和调用并不是很复杂,但是函数的参数类型和用法的确有些复杂.在此做一个小结,加深理解. Python参数的定义 负责给函数提供一些必要的数据或信息,以保证函数 ...
- window.location.href 跳转无历史记录
需求:从页面a单点登录跳至页面b,在页面b里做判断符合条件后location.href至c页面 问题:在页面c中点击返回按钮页面回到了a,正常情况下应该回到页面b 原因:在当前页面的 onload 事 ...
- <<学会提问>>第一章学习笔记
中国应不应该现在取消死刑? 中医是不是伪科学? 读书无用论? 集体主义和团队精神? 欧洲难民危机,你是支持接收难民,还是反对? 欧洲白左是不是幼稚圣母,抑或是右派种族歧视,顽固保守? 如何看待&quo ...
- ABAP术语-Fiscal Year
Fiscal Year 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/02/1062109.html Generally a period ...
- Django中ORM常用字段类型及参数
常用字段: <1> CharField 字符串字段, 用于较短的字符串. CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的 ...
- git 本地分支与远程仓库分支关联
当我们从远程仓库项目克隆到本地后,会自动创建本地master分支,并且与远程仓库主分支关联.如果我们需要在本地创建一个分支并且与远程仓库的origin/xxx分支关联,则可以通过以下命令实现 git ...