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 ...
随机推荐
- HTML5 小实例
1.时钟: <!doctype html> <html> <head></head> <body> <canvas id=" ...
- 【C语言】素数判定
题目:素数判定. 编写函数,参数是一个正整数n,如果它是素数,返回1,否则返回0. 分析 质数概念: 质数:除了1之外,只能被它本身整除的正数称为质数 如果这个数能被其他正数整除,说明这个数有两个或以 ...
- 2018 Wannafly summer camp Day3--Knight
Knight 题目描述: 有一张无限大的棋盘,你要将马从\((0,0)\)移到\((n,m)\). 每一步中,如果马在\((x,y)(x,y)\),你可以将它移动到 \((x+1,y+2)(x+1,y ...
- C++指针数组,二级指针和函数指针的练习
1.编一程序,将字符串“Hello,C++!”赋给一个字符数组, 然后从第一个字母开始间隔地输出该串(请用指针完成). 代码如下 #include<iostream> #include&l ...
- c++cmb
#include<windows.h> #include<bits/stdc++.h> using namespace std; ]; int main() { printf( ...
- hdu_1788_Chinese remainder theorem again (lcm
我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,…,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2(mod m2) … x≡ak(mod m ...
- dynamic-insert和dynamic-update属性
dynamic-insert 作用:设置对象中没有值的字段 insert并不会对其进行插入. 实体类映射配置如下 <!DOCTYPE hibernate-mapping PUBLIC &quo ...
- 介绍几个PHP 自带的加密解密函数
PHP 自带的加密解密函数 目前经常使用的加密函数有:md5(), sha1(), crypt(), base64_encode(), urlencode() . 其中 md5(), sha1(), ...
- MySQL数据操作(DML)
表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...
- Python基本图形绘制
turtle的一个画布空间最小单位是像素 turtle的绘制窗体:turtle.stup(width,heigth,startx,starty) 四个参数中后两个可选 turtle空间坐标体系:tur ...