博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
properties read write example
阅读量:4203 次
发布时间:2019-05-26

本文共 1985 字,大约阅读时间需要 6 分钟。

public class PropertyUtil01 {	private static Properties props = null;	public static void main(String[] args) {		PropertyUtil01 p;		try {			p = new PropertyUtil01();			props = readProperties();			System.out.println(readValue("name"));			System.out.println(readValue("pwd"));			setValue("url", "jdbc:db:xxx");			System.out.println();			System.out.println(p.readAllProperties());		} catch (IOException e) {			e.printStackTrace();		}	}	public Map
readAllProperties() throws FileNotFoundException, IOException { // 保存所有的键值 Map
map = new HashMap
(); Enumeration
en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key); map.put(key, Property); } Iterator
> mapEntryItr = map.entrySet().iterator(); while (mapEntryItr.hasNext()) { Map.Entry
maEntry = mapEntryItr.next(); System.out.println(maEntry.getValue()); } return map; } public static String readValue(String key) throws IOException { return props.getProperty(key); } public static void setValue(String key, String value) throws IOException { OutputStream fos = null; try { // e.g. D:\workspace\j2seTest1\bin\properties\config.properties fos = new FileOutputStream(PropertyUtil01.class.getClassLoader() .getResource("properties/").toURI().getPath() + "config.properties"); props.setProperty(key, value); props.store(fos, "comments: last update"); } catch (URISyntaxException e) { e.printStackTrace(); } finally { fos.close(); } } public static Properties readProperties() throws IOException { Properties prop = new Properties(); InputStream fis = null; try { // e.g. D:\workspace\j2seTest1\bin\properties\config.properties fis = new FileInputStream(PropertyUtil01.class.getClassLoader() .getResource("properties/").toURI().getPath() + "config.properties"); prop.load(fis); } catch (URISyntaxException e1) { e1.printStackTrace(); } finally { fis.close(); } return prop; }}

转载地址:http://xwili.baihongyu.com/

你可能感兴趣的文章
ebay 沙盒账号注册
查看>>
linux -8 Linux磁盘与文件系统的管理
查看>>
linux -8 Linux磁盘与文件系统的管理
查看>>
linux 9 -文件系统的压缩与打包 -dump
查看>>
PHP在变量前面加&是什么意思?
查看>>
ebay api - GetUserDisputes 函数
查看>>
ebay api GetMyMessages 函数
查看>>
wdlinux
查看>>
关于mysql升级到5.5报错问题
查看>>
magento - 通过storeid,分类id,天数(几天内)得到产品
查看>>
一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂!
查看>>
Redis和Memcache对比及选择
查看>>
用谷歌账号登陆magento、
查看>>
php oauth 模块在linux下安装
查看>>
代替nginx的服务器 - The Tengine Web Server
查看>>
nginx 升级成 tengine 的代码
查看>>
magento rest api 调用
查看>>
magento rest api 调用!
查看>>
magento rest api get Token key and secret
查看>>
谷歌获取货币汇率代码
查看>>