JAVA操作属性文件,可进行读、写、更改

  
JAVA 操作属性文件
/*
操作属性文件,可以为我们的程序带来更方便的移植性,下面是一个示例,可以读、写、更改属性
读采用了两种方式,一种是采用 Properties 类,另外一种是采用资源绑定类 ResourceBundle 类,
下面是源程序,里面有详细的注释:
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;
/**
  * 对属性文件( xx.properties )的操作
  * 注:属性文件一定要放在当前工程的根目录下,也就是放在与 src 目录在同一个目录下(我的 JDevelop
  * 是这样的)
  */
public class OperatePropertiesFile {
    public OperatePropertiesFile() {
    }
    /**
     * 采用 Properties 类取得属性文件对应值
     * @param propertiesFileName properties 文件名,如 a.properties
     * @param propertyName 属性名
     * @return 根据属性名得到的属性值,如没有返回 ""
     */
    private String getValueByPropertyName(String propertiesFileName,String propertyName) {
        String s= "" ;
        Properties p= new Properties(); // 加载属性文件读取类
        FileInputStream in;
        try {
            //propertiesFileName test.properties
            in = new FileInputStream(propertiesFileName); // 以流的形式读入属性文件
            p.load(in); // 属性文件将该流加入的可被读取的属性中
            in.close(); // 读完了关闭
            s=p.getProperty(propertyName); // 取得对应的属性值
        } catch (Exception e) {
            e.printStackTrace();
        }
         return s;
    }
    /**
     * 采用 ResourceBundel 类取得属性文件对应值,这个只能够读取,不可以更改及写新的属性
     * @param propertiesFileNameWithoutPostfix properties 文件名,不带后缀
     * @param propertyName 属性名
     * @return 根据属性名得到的属性值,如没有返回 ""
     */
    private String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
        String s= "" ;
        // 如属性文件是 test.properties ,那此时 propertiesFileNameWithoutPostfix 的值就是 test
        ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
        s=bundel.getString(propertyName);
        return s;
    }
    /**
     * 更改属性文件的值,如果对应的属性不存在,则自动增加该属性
     * @param propertiesFileName properties 文件名,如 a.properties
     * @param propertyName 属性名
     * @param propertyValue 将属性名更改成该属性值
     * @return 是否操作成功
     */
    private boolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
        boolean writeOK= true ;
        Properties p= new Properties();
        FileInputStream in;
        try {
            in = new FileInputStream(propertiesFileName);
            p.load(in); //
            in.close();
            p.setProperty(propertyName,propertyValue); // 设置属性值,如不属性不存在新建
            //p.setProperty("testProperty","testPropertyValue");
            FileOutputStream out= new FileOutputStream(propertiesFileName); // 输出流
            p.store(out, "Just Test" ); // 设置属性头,如不想设置,请把后面一个用 "" 替换掉
            out.flush(); // 清空缓存,写入磁盘
            out.close(); // 关闭输出流
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writeOK;
    }
    public static void main(String[] args) {
        OperatePropertiesFile operatePropertiesFile = new OperatePropertiesFile();
        operatePropertiesFile.changeValueByPropertyName( "db.properties" , "DBLocation" , "D://Palfinger//palfinger.mdb" );
    }
}
假如有一个属性文件 db.properties如下:  
DBLocation=D/://Palfinger//palfinger.mdb
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值