文件的写入:
直接通过FileUtils来保存键值对,示例如下:
Value a=Value(10);
Value b=Value("Hello");
ValueMap valueMap;
valueMap.insert(std::make_pair("number",a));
valueMap.insert(std::make_pair("name",b));
std::string filepath=FileUtils::getInstance()->getWritablePath();//获取可写路径,只能在可写路径下进行文件的写入
log("path is %s",filepath.c_str());//打印可写路径
FileUtils::getInstance()->writeToFile(valueMap,filepath+"/hh.plist");
//用模拟器console下查看产生的hh.plist文件的路径和文件内容如下:
localhost:Documents wangwei$ pwd
/Users/wangwei/Library/Developer/CoreSimulator/Devices/84781560-37A5-4F81-B2E6-39DD8C8757B6/data/Containers/Data/Application/4EB4CD53-4574-4ED6-A82F-FBA88C920ABF/Documents
localhost:Documents wangwei$ ls
hh.plist
localhost:Documents wangwei$ more hh.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Hello</string>
<key>number</key>
<integer>10</integer>
</dict>
</plist>
用真机调试的文件路径如下:
path is /var/mobile/Containers/Data/Application/2F159D86-297F-4ECA-A8A0-CD35287078AF/Documents/
文件的读取:
ValueMap read_valueMap;
read_valueMap=FileUtils::getInstance()->getValueMapFromFile(filepath+"/hh.plist");
std::string name=valueMap.at("name").asString();
log("read name is %s",name.c_str());