package example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 读取properties配置文件
*/
public class Configuration {
private Properties pro;
private FileInputStream fileInputStream;
private FileOutputStream fileOutputStream;
private String filepath;
public Configuration() {
//重要内容
//测试地址
filepath="D:\\config.properties";
pro = new Properties();
try {
fileInputStream = new FileInputStream(filepath);
pro.load(fileInputStream);
fileInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 得到文件路径
*/
public String getfilepath() {
return filepath;
}
/**
* 根据key取得键值
*/
public String getValue(String key) {
if (pro.containsKey(key)) {
String value = pro.getProperty(key);
return value;
} else {
return "";
}
}
/**
* 改变或者添加一个key的值
* 当key存在于properties文件中时,修改key的值, 当key不存在时,添加键值对
* @param key:要存入的键
* @param value:要存入的值
*/
public void setValue(String key, String value) {
pro.setProperty(key, value);
}
/**
* 将更改后的文件数据存入指定的文件中
*/
public void saveFile(String fileName,String comments) {
try {
fileOutputStream = new FileOutputStream(fileName);
pro.store(fileOutputStream, "");
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 测试方法
*/
public static void main(String[] args) {
String filename="D:\\config.properties";
Configuration conf = new Configuration();
conf.setValue("timeInterval","33");
conf.setValue("filepath","bb");
conf.saveFile(filename,"test");
String timeInterval= conf.getValue("timeInterval");
System.out.println(timeInterval);
String filepath = conf.getValue("filepath");
System.out.println(filepath);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
共3个文件
java:2个
properties:1个
3星 · 超过75%的资源 需积分: 50 37 下载量 55 浏览量
2013-08-14
17:10:08
上传
评论 1
收藏 1KB RAR 举报
温馨提示
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
资源推荐
资源详情
资源评论
收起资源包目录
properties实例.rar (3个子文件)
config.properties 95B
configuration1.java 105B
configuration.java 2KB
共 3 条
- 1
ttha
- 粉丝: 18
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页