文件存储操作--字符串

简单封装 字符串的存储操作,省去中间沙盒处理方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//存储publickey和sessionID -- writeContent: nil - 仅取出数据, 其他 - 修改原内容并提取
+( NSString *)storeFile:( NSString *)fileName content :( NSString *)writeContent
{
     NSString *pathDocuments=[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES ) objectAtIndex : 0 ];
     
     NSString *createPath=[ NSString stringWithFormat : @"%@/%@" ,pathDocuments,fileName]; //用文件名补全路径
     NSError * ReadFileError ;
     NSString *readContent ;
     NSData *data ;
     if ([[ NSFileManager defaultManager ] fileExistsAtPath :createPath]) //判断文件是否已存在
     {
         if (nil == writeContent) {
             readContent = [ NSString stringWithContentsOfFile :createPath encoding : NSUTF8StringEncoding error :& ReadFileError ;];
         } else {
             data = [writeContent dataUsingEncoding :NSUTF 8 StringEncoding]; //新文件的初始数据
             [[ NSFileManager defaultManager ] createFileAtPath :createPath contents :data attributes :nil ]; //创建文件
             readContent = [ NSString stringWithContentsOfFile :createPath encoding : NSUTF8StringEncoding error :& ReadFileError ;];           
         }
     }
     else
     {
         if (nil == writeContent) {
             return nil ;
         } else {
             data = [writeContent dataUsingEncoding :NSUTF 8 StringEncoding]; //新文件的初始数据
             [[ NSFileManager defaultManager ] createFileAtPath :createPath contents :data attributes :nil ]; //创建文件
             readContent = [ NSString stringWithContentsOfFile :createPath encoding : NSUTF8StringEncoding error :& ReadFileError ;];
         }
     }
     return readContent;
}

你可能感兴趣的:(存储)