java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
来源:互联网 发布:网站接入微信支付源码 编辑:程序博客网 时间:2024/06/06 22:14
#Tue Aug 13 15:30:56 CST 2013
timeInterval=33
name=holdOn
filepath=bb
ip=192.168.1.1
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);
}
}
提示:实例可以正常运行,重要内容(获取项目中properties文件路径)被省略,可能是您需要的,有需要的,给我发邮件,我把完整java实例打包回发给您。
我的邮箱:qmys116505@qq.com
- java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
- 关于读取properties文件找不到路径问题
- 解决Pycharm 文件更改目录后,执行路径未更新的问题
- 解决Visual Studio编译出现Moc'ing文件,系统找不到指定路径问题
- 解决Visual Studio编译出现Moc'ing文件,系统找不到指定路径问题
- 解决VS编译出现Moc'ing文件,系统找不到指定路径问题
- 解决Visual Studio编译出现Moc'ing文件,系统找不到指定路径问题
- Java读写.properties文件实例,解决中文乱码问题
- Java读写.properties文件实例,解决中文乱码问题
- Java properties | FileNotFoundException: properties (系统找不到指定的文件。)
- Java读取properties文件中文乱码问题的解决
- 解决Java读取properties文件中文乱码的问题
- Java读取、写入文件如何解决乱码问题
- 一个properties文件读写组件类,解决java自带的properties文件处理薄弱问题
- Java 读取properties文件中文乱码解决
- 解决Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径)问题
- 解决Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)问题
- 解决Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)问题
- 计算机常用英语术语
- Caml语句Loopup类型类型查询
- 微信公共平台Node.js发送微信 获取fakeID
- iOS in Practice iOS 6实践指南全文 中文下载
- 四、CXF WebService中传递复杂类型对象
- java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
- Linux下串口操作之数据拼接
- 移动端webapp开发必备知识
- android - anim translate中 fromXDelta、toXDelta、fromYDelta、toXDelta属性
- SSH login without password
- 数字图像处理 读书笔记二 眼睛的构造
- MyEclipse中部署调试 JeeCms(v2.4.2)项目
- Proxy 模式
- D3D中材质通过D3DMATERIAL9结构说明