向文件中写入和读取属性(Property)信息

主要是Properties类和文件流之间的操作,在文件中写入属性信息后,其中包含修改时间信息和 键值对数据,代码:

1.从指定文件读取属性信息:

	public Properties get() {
		FileInputStream fis = null;
		Properties props = new Properties();
		try{
			
			//读取app_config目录下的config
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			fis = new FileInputStream(dirConf.getPath() + File.separator + APP_CONFIG);
			
			props.load(fis);
		}catch(Exception e){
		}finally{
			try {
				fis.close();
			} catch (Exception e) {}
		}
		return props;
	}

APP_CONFIG是字符串,代表文件名,这里如果不存在会报异常

2.向指定文件写入属性信息:
	private void setProps(Properties p) {
		FileOutputStream fos = null;
		try{
			
			//把config建在(自定义)app_config的目录下
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			File conf = new File(dirConf, APP_CONFIG);
			fos = new FileOutputStream(conf);
			
			p.store(fos, null);
			fos.flush();
		}catch(Exception e){	
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (Exception e) {}
		}
	}

APP_CONFIG 如果不存在会自动创建

3.对属性键值对的修改:
	public void add(Properties ps)
	{
		Properties props = get();
		props.putAll(ps);
		setProps(props);
	}
	
	public void set(String key,String value)
	{
		Properties props = get();
		props.setProperty(key, value);
		setProps(props);
	}
	
	public void remove(String...key)
	{
		Properties props = get();
		for(String k : key)
			props.remove(k);
		setProps(props);
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例类,演示了如何封装一个支持重载读取写入节点属性的类: ```cpp #include <iostream> #include <string> #include "pugixml.hpp" class XMLNode { public: XMLNode(const std::string& filePath) { document.load_file(filePath.c_str()); } template<typename T> T getProperty(const std::string& nodeName, const std::string& attributeName) { pugi::xml_node node = document.child(nodeName.c_str()); if (node) { pugi::xml_attribute attr = node.attribute(attributeName.c_str()); if (attr) { return attr.as_string(); } } // 返回默认值或抛出异常,根据需要自行决定 return T(); } template<typename T> void setProperty(const std::string& nodeName, const std::string& attributeName, const T& value) { pugi::xml_node node = document.child(nodeName.c_str()); if (!node) { // 创建新节点 node = document.append_child(nodeName.c_str()); } node.append_attribute(attributeName.c_str()).set_value(value); } void save(const std::string& filePath) { document.save_file(filePath.c_str()); } private: pugi::xml_document document; }; int main() { XMLNode xmlNode("example.xml"); // 读取属性 std::string author = xmlNode.getProperty<std::string>("book", "author"); int year = xmlNode.getProperty<int>("book", "year"); std::cout << "Author: " << author << std::endl; std::cout << "Year: " << year << std::endl; // 修改属性 xmlNode.setProperty("book", "author", "John Doe"); xmlNode.setProperty("book", "year", 2022); // 保存修改后的内容 xmlNode.save("modified_example.xml"); return 0; } ``` 在这个示例,XMLNode类封装了一个pugixml的xml_document对象,并提供了getProperty()和setProperty()成员函数来读取写入节点属性。它使用了模板函数,可以支持不同类型的属性值。在main函数,我们创建了一个XMLNode对象,加载了一个XML文件,并演示了如何读取属性、修改属性和保存修改后的内容。 请注意,这只是一个简单的示例,你可以根据自己的需求对XMLNode类进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值