java 写文件 属性吗_java从properties文件中读取和写入属性

1 //ConfigUtil.java

2 packageliweiTest;3 importjava.io.File;4 importjava.io.FileNotFoundException;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8 importjava.net.URISyntaxException;9 importjava.net.URL;10 importjava.util.HashMap;11 importjava.util.Map;12 importjava.util.Properties;13 importjava.util.Set;14 public classConfigUtil15 {16

17 private static Properties props = null;18 static

19 {20 props = newProperties();21 reLoad();22 }23 //加载属性文件

24 private static voidreLoad()25 {26 try

27 {28 InputStream input = ConfigUtil.class.getResourceAsStream("/configuration.properties");29 props.load(input);30 }31 catch(Exception ex)32 {33 System.out.println("exceptiom:"+ex);34 }35 }36 //获取当前文件的路径,返回成FileOutputStream

37 public staticFileOutputStream getUrl()38 {39 String url = ConfigUtil.class.getClassLoader().getResource(".").getPath();40 FileOutputStream file = null;41 try

42 {43 file = new FileOutputStream(url + File.separator + "configuration.properties");44 }45 catch(FileNotFoundException e)46 {47 e.printStackTrace();48 } //指定要操作的文件

49 returnfile;50 }51

52 //获取某个键的属性

53 public staticString getString(String name)54 {55 try

56 {57 returnprops.getProperty(name);58 } catch(Exception ex)59 {60 System.out.println("exceptiom:"+ex);61 return null;62 }63 }64

65 /**

66 *@paramname 名称67 *@paramdef 参数68 *@return结果69 */

70 public staticString getString(String name, String def)71 {72 String retval = null;73 try

74 {75 retval =props.getProperty(name);76 retval = retval == null ?def : retval;77 }78 catch(Exception ex)79 {80 logger.error("", ex);81 }82 returnretval;83 }84

85 //对configuration.properties进行遍历,获取全部的键值和属性值

86 public static HashMapgetAll()87 {88 Set names =props.stringPropertyNames();89 HashMap map = new HashMap();90 for(String name:names)91 {92 map.put(name, props.getProperty(name));93 }94 returnmap;95 }96

97 //写入一对属性到 configuration.properties文件中

98 public static voidsetValue(String name,String value)99 {100 props.setProperty(name,value) ; //设置属性

101 try

102 {103 props.store(getUrl(),"放入一对属性");104 }105 catch(IOException e)106 {107 e.printStackTrace();108 } //保存属性到普通文件

109 }110

111 //同时写入多个属性到 configuration.properties文件中

112 public static void setAllValue(Mapmap)113 {114 for(Map.Entryme:map.entrySet())115 {116 props.setProperty(me.getKey(),me.getValue()) ; //设置属性

117 }118

119 try

120 {121 props.store(getUrl(),"放入多个属性");122 }123 catch(IOException e)124 {125 e.printStackTrace();126 } //保存属性到普通文件

127 }128 public static voidmain(String[] args)129 {130 System.out.println(ConfigUtil.getString("name"));131 System.out.println(ConfigUtil.getString("name2","qwertyuiop"));132 setValue("姓名", "李WEI123");133 HashMap map = new HashMap();134 map.put("driver","oracle");135 map.put("value", "127.0.0.1");136 map.put("作用", "用来测试玩的,呵呵...");137 setAllValue(map);138 System.out.println(ConfigUtil.getString("姓名"));139 HashMap map2 =getAll();140 for(Map.Entrym:map2.entrySet())141 {142 System.out.println(m.getKey()+" = "+m.getValue());143 }144 }145 }146

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值