properties的读写(IO操作)及值修改

最笨最实在思路最全面的做法,用于最基础的代码锻炼非常有效,推荐给大家打打java底^_^
None.gif package cn.net.withub.xfgl.common;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/***/ /**
InBlock.gif*
@authorYuLuo
ExpandedBlockEnd.gif
*/

None.gif
None.gif
import java.io.File;
None.gif
import java.io.FileInputStream;
None.gif
import java.io.FileNotFoundException;
None.gif
import java.io.FileOutputStream;
None.gif
import java.io.IOException;
None.gif
import java.io.InputStream;
None.gif
import java.util.Properties;
None.gif
// 前一篇已介绍此方法
None.gif
import cn.net.withub.xfgl.common.ToUnicode;
None.gif
ExpandedBlockStart.gif
public class CreatePro dot.gif {
InBlock.gif
//读文件
InBlock.gif
publicstaticStringreadFile(StringfilePath,StringparameterName)throwsIOException,FileNotFoundExceptiondot.gif{
InBlock.gifStringresult
=null;
InBlock.gifFilefile
=newFile(filePath);
if(file.exists())dot.gif{
InBlock.gif FileInputStreamfis
=newFileInputStream(file);
InBlock.gif
byte[]b=newbyte[fis.available()];
InBlock.gif fis.read(b);
InBlock.gif result
=newString(b,"UTF-8");
InBlock.gif fis.close();
ExpandedSubBlockEnd.gif }

InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

InBlock.gif
//修改后存储
InBlock.gif
publicstaticvoidsaveFile(Stringcontent,Stringpath,StringfileName)throwsIOExceptiondot.gif{
InBlock.gif Filef
=newFile(path);
ExpandedSubBlockStart.gif
if(!f.exists())dot.gif{
InBlock.gif f.mkdirs();
ExpandedSubBlockEnd.gif }

InBlock.gif Filefn
=newFile(f,fileName);
InBlock.gif FileOutputStreamfos
=newFileOutputStream(fn);
InBlock.gif fos.write(content.getBytes());
InBlock.gif fos.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
//删除旧文件
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticvoiddeleteFile(Stringpath)throwsIOExceptiondot.gif{
InBlock.gif Filef
=newFile(path);
ExpandedSubBlockStart.gif
if(f.exists())dot.gif{
InBlock.gif f.delete();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
InBlock.gif
thrownewIOException("未找到相关文件");
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

InBlock.gif
//执行方法
InBlock.gif
publicstaticbooleanwriteProperties(StringfilePath,StringparameterName,StringparameterValue)dot.gif{
InBlock.gif
booleanflag=false;
ExpandedSubBlockStart.gif
trydot.gif{
InBlock.gif
//取得文件所有内容
InBlock.gif
Stringall=CreatePro.readFile(filePath,parameterName);
InBlock.gif Stringresult
=null;
InBlock.gif
//如果配置文件里存在parameterName
ExpandedSubBlockStart.gif
if(all.indexOf(parameterName)!=-1)dot.gif{
InBlock.gif
//得到parameterName前的字节数
InBlock.gif
inta=all.indexOf(parameterName);
InBlock.gif
//取得以前parameterName的值
InBlock.gif
Stringold=readProperties(filePath,parameterName);
InBlock.gif
//得到parameterName值前的字节数
InBlock.gif
intb=a+(parameterName.length()+"=".length());
InBlock.gif
//新的properties文件所有内容为:旧的properties文件中内容parameterName+"="+新的parameterName值(parameterValue)+旧的properties文件中parameterName值之后的内容
InBlock.gif
result=all.substring(0,a)+parameterName+"="+parameterValue+all.substring(b+ToUnicode.convert(old).length(),all.length());
ExpandedSubBlockEnd.gif }

InBlock.gif
//删除以前的properties文件
InBlock.gif
CreatePro.deleteFile(filePath);
InBlock.gif
//存储新文件到以前位置
InBlock.gif
String[]arrPath=filePath.split("WEB-INF");
InBlock.gif Stringpath
=arrPath[0]+"WEB-INF\\configs";
InBlock.gif CreatePro.saveFile(result,path,
"xf.properties");
InBlock.gif flag
=true;
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(IOExceptione)dot.gif{
InBlock.gif e.printStackTrace();
InBlock.gif flag
=false;
ExpandedSubBlockEnd.gif }

InBlock.gif
returnflag;
ExpandedSubBlockEnd.gif}

InBlock.gif
//Properties方法写properties文件
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Propertiesprop=newProperties();
InBlock.giftry{
InBlock.gifInputStreamfis=new
InBlock.gifFileInputStream(filePath);
InBlock.gif//从输入流中读取属性列表(键和元素对)prop.load(fis);
InBlock.gif//调用Hashtable的方法put。使用getProperty方法提供并行性。
InBlock.gif//强制要求为属性的键和值使用字符串。返回值是Hashtable调用put的结果。
InBlock.gifOutputStreamfos=newFileOutputStream(filePath);
InBlock.gifprop.setProperty(parameterName,parameterValue);
InBlock.gif//以适合使用load方法加载到Properties表中的格式,
InBlock.gif//将此Properties表中的属性列表(键和元素对)写入输出流
InBlock.gifprop.store(fos,"Update'"+parameterName+"'value\n");
InBlock.giffalg=true;
InBlock.gif}catch(IOExceptione){
InBlock.gife.printStackTrace();
InBlock.gifSystem.err.println("Visit"+filePath+"forupdating"+parameterName+"valueerror");
InBlock.giffalg=false;
ExpandedSubBlockEnd.gif}
*/

InBlock.gif
//读properties文件,Properties方法
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticStringreadProperties(StringfilePath,StringparameterName)dot.gif{
InBlock.gif Stringvalue
="";
InBlock.gif Propertiesprop
=newProperties();
ExpandedSubBlockStart.gif
trydot.gif{
InBlock.gif InputStreamfis
=newFileInputStream(filePath);
InBlock.gif prop.load(fis);
InBlock.gif value
=prop.getProperty(parameterName);
ExpandedSubBlockStart.gif }
catch(IOExceptione)dot.gif{
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

InBlock.gif
returnvalue;
ExpandedSubBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值