java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题...

properties属性文件:config.properties
 
#
#Tue Aug 13 15:30:56 CST 2013
timeInterval=33
name=holdOn
filepath=bb
ip=192.168.1.1
 
  类实例:Configuration.java 
package example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.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;
 }
 
 public String getValue(String key) {
  if (pro.containsKey(key)) {
   String value = pro.getProperty(key);
   return value;
  } else {
   return "";
  }
 }

 
 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
  

 

 

 

 

 

 

 

 

 

 

 

 

 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值