java中属性文件编写_在java中关于properties文件类型的定义及实例分析

前言

在java中使用Properties类对properties文件进行读写,该文件类型是“键-值”形式进行存放,在读写文件时,使用ISO-8859-1编码,所有字符是Latin字符,对于中文需要转码后进行读写,否则会出现乱码,在jdk的bin目录下面提供native2ascii.exe将中文进行转码,如下图1-1所示。

2d133cef55e2bceecdbbcd19089ccae2.png

图1-1

一、读配置文件/**@b@ * 根据指定key读取value@b@ * @param fileName@b@ * @param key:属性名称@b@ * @retrun:属性值@b@ */@b@ pubic String  read(String fileName,String key){@b@    Properties props=new Properties():@b@    try{@b@        props.load(this.getClass().getClassLoader().getResourceAsSTream(fileName));@b@        String n=props.getProperty(key);@b@        System.out.println(n);@b@        return n;@b@    }catch(Exception e){@b@        e.printStackTrace@b@        return nll;@b@    }@b@@b@}@b@@b@public void readProperties(String fileName){@b@    Properties props=new properties();@b@@b@try{@b@        props.load(this.getClass().getClassLoader().getResourceAsSTream(fileName));@b@        String n=props.getProperty(key);@b@        System.out.println(n);@b@        return n;@b@    }catch(Exception e){@b@        e.printStackTrace@b@        return nll;@b@    }@b@    @b@ public  void readProperties(String fileName ){@b@     Properties props=new Properties();@b@     try{@b@         Enumeration en=props.propertyName;@b@         while(en.hasMoreElement){@b@             String key=(String)en.nextElemnent();@b@             System.out.println(key+":"+props.getProperty(key));@b@         }@b@     }catch(Exception e){}   @b@ }

二、写配置文件/**@b@ * 根据指定key读取value@b@ * @param fileName@b@ * @param parameterName:属性名称@b@ * @param:parameterValue属性值 */@b@ public void writeProperties(String fileName,String parameterName,String parameterValue){@b@     @b@     Properties prop=new Properties();@b@     String path=this.getClass().getClassLoader().getResource("").getPath().substring(1);@b@     path=path+fileName;@b@     System.out.println(fileName);@b@     try{@b@         InputStream fis=new FileInputStream(path);@b@         prop.load(fis);@b@         OutputStream fos=new FileOutputStream(path);@b@         prop.setProperty(paramerterName,parameterValue);@b@         prop.store(fos,"Update"+parameterName+"value");@b@         @b@     }catch(Exception e){@b@     }@b@ }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值