java 写property文件_java笔记----property文件读写

packagecom.test.property;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Properties;/***@author*@version创建时间:2019年3月26日 上午8:40:17

* 类说明*/

public classPropertyTest {static Properties prop = newProperties();static String strPath=PropertyTest.class.getClassLoader().getResource("./").getPath()+"/a.properties";static HashMap map = new HashMap();public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub

File file= newFile(strPath);

String[] path=strPath.split("/");

String filename=path[path.length-1];

String dpath=strPath.replace("/"+filename, "");

System.out.println("文件路径"+dpath);

System.out.println("文件名"+filename);if(!newFile(dpath).exists()){newFile(dpath).mkdirs();

System.out.println("创建目录成功:"+dpath);if(!file.exists()){

file.createNewFile();

System.out.println("创建文件成功:"+file);

}

}else{if(!file.exists()){

file.createNewFile();

System.out.println("创建文件成功2:"+file);

}

}//读取属性文件a.properties

InputStream in = new BufferedInputStream (newFileInputStream(strPath));

prop.load(in);///加载属性列表

Iterator it=prop.stringPropertyNames().iterator();while(it.hasNext()){

String key=it.next();

map.put(key,prop.getProperty(key));

System.out.println(key+":"+prop.getProperty(key));

}

in.close();

addProp("3","455");

}private static boolean addProp(String key,String value){//添加key不是相同的property//写

if(map.containsKey(key)){ //里面含有该key不写进去

return false;

}else{try{

FileOutputStream oFile= new FileOutputStream(strPath, false);

prop.setProperty(key, value);

prop.store(oFile,null);//null就是不要注释

oFile.close();return true;

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}//true表示追加打开

return false;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值