项目运行过程中修改.properties文件问题
2015-04-07 08:47
141 查看
项目运行过程中需要修改配置文件的代码。菜鸟贴出记录自己成长。。
/**
showOu 要修改为的值
*/
public void setAttribute(String value) {
OutputStream out = null;
InputStream in = null;
if(showOu == null){
showOu = "ou";
}
try {
//获取服务器上配置文件所在位置
File file = new File(com.avicit.tools.GetSystemPath.getClassesPath() + "show.properties");
if (!file.exists())
file.createNewFile();
in = new FileInputStream(file);
Properties p = new Properties();
p.load(in);
in.close();
p.setProperty("Attr_name", value);
//out的位置很重要。一定要在setProperties之后。不然会把原来的所有其他属性覆盖掉
out = new FileOutputStream(file);
//将修改后的文件存回去
p.store(out, null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
注意每次修改后的配置文件在本地是不会变的,因为路径获取的是服务器上的路径,修改的也为服务器上的配置文件。
如果在项目运行过程中修改了配置文件,则再次读取配置文件之前要重新从服务器上读取一次,因为会有配置文件的缓存,不再次读取的话还是修改以前的值。
/**
showOu 要修改为的值
*/
public void setAttribute(String value) {
OutputStream out = null;
InputStream in = null;
if(showOu == null){
showOu = "ou";
}
try {
//获取服务器上配置文件所在位置
File file = new File(com.avicit.tools.GetSystemPath.getClassesPath() + "show.properties");
if (!file.exists())
file.createNewFile();
in = new FileInputStream(file);
Properties p = new Properties();
p.load(in);
in.close();
p.setProperty("Attr_name", value);
//out的位置很重要。一定要在setProperties之后。不然会把原来的所有其他属性覆盖掉
out = new FileOutputStream(file);
//将修改后的文件存回去
p.store(out, null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
注意每次修改后的配置文件在本地是不会变的,因为路径获取的是服务器上的路径,修改的也为服务器上的配置文件。
如果在项目运行过程中修改了配置文件,则再次读取配置文件之前要重新从服务器上读取一次,因为会有配置文件的缓存,不再次读取的话还是修改以前的值。
相关文章推荐
- 在项目修改过程中永远要保证可运行版本
- 运行jar包提示找不到.properties文件的问题
- 导入SourceSafe过程中项目结构无法修改问题的解决方案。
- 项目移植过程中,gen目录中R文件更新问题
- maven+jetty项目 启动后 无法修改加载后的资源文件问题
- 在项目修改过程中永远要保证可运行版本
- 更改J2E项目名称后运行报错问题修改?
- 解决修改properties 属性文件存在缓存问题,附带操作properties文件工具类
- 【开发过程问题汇总系列】【定时器】Timer运行的过程中把系统时间修改为以前的时间会停止运行的问题
- 试图运行项目时出错 未能加载文件或程序集 问题
- 设置eclipse在编译运行项目之前自动保存修改的文件
- sakai修改sakai.properties的字段为中文运行后出现乱码的问题分析与解决方案!
- 解决修改properties 属性文件存在缓存问题,附带操作properties文件工具类
- 项目过程中MyEclipse出问题----MyEclipse中修改java code,不起作用
- 在前台页面修改properties配置文件后 tomcat重新加载该配置文件的问题
- 解决项目中hbm文件用maven运行没有拷贝到target里面的问题
- 关于系统读取properties配置文件的路径问题,包括打成jar包的运行文件
- 如何导出Java项目成为一个可以直接双击运行的jar文件的问题
- 加入log4j.properties项目运行日志管理,将日志文件保存在硬盘文件夹中