java中经常用到的读写配置文件的信息properties属性的写入,读取例子

package com.util;
import java.io.*;
import java.util.*;
/**
 *
 * @author zhaizhanpo
 *
 *
 */

public class ReadWriteProUtil {
   
 
 /**
  * 根据key读取value
  * 在工程中取得相对路径的方法 :this.getClass().getResource("工程路径").getPath();
  * @param filePath
  * @param key
  * @return
  */
    public static String readValue(String filePath, String key){
        Properties props = new Properties();
        FileInputStream in=null;
        try{
            in = new FileInputStream(filePath);
            props.load(in);
            String value = props.getProperty(key);
            return value;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally{
         try {
    in.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
        }
    }
   
    /**
     * 读取properties的全部信息
     * @param filePath 文件的路径
     */
    public static void readProperties(String filePath){
        Properties props = new Properties();
        InputStream in=null;
        try{
            in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            Enumeration en = props.propertyNames();
            while(en.hasMoreElements()){
                String key = (String)en.nextElement();
                String property = props.getProperty(key);
                System.out.println(key + " : " + property);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
         try {
    in.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
        }
    }
   
    /**
     * 写入properties信息
     * @param filePath 要写入的文件的路径
     * @param parameterName 要改变的值得件
     * @param parameterValue 要改成的内容
     */
   
    public static void writeProperties(String filePath, String parameterName, String parameterValue){
        Properties props = new Properties();
        OutputStream fos=null;
        InputStream fis =null;
        try{
            fis = new FileInputStream(filePath);
            //从输入流中读取属性列表(键和元素对)
            props.load(fis);
             fis.close();
            fos = new FileOutputStream(filePath);
            props.setProperty(parameterName, parameterValue);
            //以适合使用load方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素对)写入输出流

            //加载额外的内容
            String otherContent="making people zhaizhanpo";
            props.store(fos,otherContent);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
         try {
    fis.close();
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }       
        }
    }

    public static void main(String[] args) {
     // example 更改用户名称
     String filepath="D://Test//src//pro.txt";
     ReadWriteProUtil.writeProperties(filepath,"userName","000");
     //输出更改后的信息。
        System.out.println(ReadWriteProUtil.readValue(filepath,"userName"));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值