ios数据存储NSString,NSArray,NSDictionary

来源:互联网 发布:数据库的服务器名称 编辑:程序博客网 时间:2024/05/23 12:41
    //获取文件路径,路径名不可随意取,不可以光使用文件名作为路径    NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);        NSString*directory=[paths objectAtIndex:0];        NSString*path=[NSString stringWithFormat:@"%@/data.txt",directory ];                NSArray* arr=@[@"abc",@"def",@"xxx"];    [arr writeToFile:path atomically:YES];        NSArray*dats= [NSArray arrayWithContentsOfFile:path];    NSLog(@"读取Array文件内容 ---- ");    for (NSString *s in dats) {       NSLog(@"%@",s);    }            NSDictionary*map=@{@"k1":@"v1",@"k2":@"v2"};    [map writeToFile:path atomically:YES];        NSDictionary* datamap=[NSDictionary dictionaryWithContentsOfFile:path];        NSLog(@"读取Directory文件内容 ---- ");    NSLog(@"%@",datamap);            [@"abc-str-xxx" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];    NSString*s=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"读取NSString文件内容 ---- %@",s);    

打印如下

2015-10-10 20:57:59.697 table[9594:708660] Unknown class ViewController in Interface Builder file.2015-10-10 20:57:59.731 table[9594:708660] 读取Array文件内容 ---- 2015-10-10 20:57:59.731 table[9594:708660] abc2015-10-10 20:57:59.732 table[9594:708660] def2015-10-10 20:57:59.732 table[9594:708660] xxx2015-10-10 20:57:59.735 table[9594:708660] 读取Directory文件内容 ---- 2015-10-10 20:57:59.736 table[9594:708660] {    k1 = v1;    k2 = v2;}2015-10-10 20:57:59.739 table[9594:708660] 读取NSString文件内容 ---- abc-str-xxx


0 0
原创粉丝点击