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 ...
随机推荐
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 指纹获取 Fingerprint2
指纹插件 Fingerprint2 import Fingerprint2 from 'fingerprintjs2' new Fingerprint2().get(function(result, ...
- Linux mongodb安装、启动、运行
1.下载 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.4.tgz 2.安装 tar -zxvf ...
- LeetCode 中级 - 翻转矩阵后的得分(861)
有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后,将该矩阵的每一 ...
- 【PTA 天梯赛训练】电话聊天狂人(简单map)
输入格式: 输入首先给出正整数N(≤10^5),为通话记录条数.随后N行,每行给出一条通话记录.简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔. 输出格式: 在一行中给出 ...
- 【杂题总汇】HDU-5215 Cycle
◆HDU-5215◆ Cycle 国庆节集训的第三天……讲图论,心情愉快……刷了一堆水题,不过也刷了一些有意思的题 +传送门+ HDU ▶ 题目 给出一个无向图(无自环,无重边),求该无向图中是否存在 ...
- ABAP术语-Purchase Order
Purchase Order 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/07/1094717.html Request or instr ...
- ABAP术语-Business Connector
Business Connector 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/27/1016379.html XML-based st ...
- Apache和Nignx基于三种方式搭建web站点并设置用户访问控制达到优化整个站点性能
个人用户主页: 1:Vim /etc/http/con.d/userdir: UserDir disabled //个人用户主页开启 UserDir public_html //指定 ...