文件的寫入:
直接通過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());