NutzCN Logo
问答 PropertiesProxy怎么写入属性
发布于 2569天前 作者 qq_64f81159 2038 次浏览 复制 上一个帖子 下一个帖子
标签:

PropertiesProxy pp = new PropertiesProxy("custom/"); 这个可以获取 但是貌似没有写入的方法,只看到一个print,传入的是writer,这个我试了下结果貌似没啥效果。

4 回复

你自己也说是读取配置的类,不是写入的。

貌似是这样才行

public static void main(String[] args) {
		FileWriter writer = null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("db.url", "123");
		map.put("db.username", "jdbc:oracle:456");
		map.put("db.password", "789");
		map.put("db.validationQuery", "select 1 from dual");
		map.put("db.maxActive", "100");
		String fileName = "d:/custom/db.properties";
		PropertiesProxy propertiesProxy = new PropertiesProxy();
		propertiesProxy.putAll(map);
		try {
			writer = new FileWriter(fileName);
			propertiesProxy.print(writer);
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{   
			if (writer != null)
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}  
        }  
	}

貌似这样才可以

恩? 我试试

果然有bug, 你这样写吧, writer是FileWriter

        String NL = System.getProperty("line.separator");
        for (Map.Entry<String, String> en : entrySet()) {
            writer.write(en.getKey());
            String val = en.getValue();
            writer.write('=');
            writer.write(val);
            writer.write(NL);
        }
        writer.flush();

添加回复
请先登陆
回到顶部