直接上代码:复制粘贴 自己看 懒得解释#24
NSString*content =@"将字符串存储到本地文件xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
//(2)拼接上要存储的文件路径(前面自动加上/),如果没有此文件系统会自动创建一个
NSString*newFielPath = [documentsPathstringByAppendingPathComponent:@"aa.txt"];
//(3)将内容存储到指定文件路径
NSError*error =nil;
//字符串写入本地文件参数:(1)要存储内容的文件路径,(2)是否使用原子特性,(3)存储格式
BOOLisSucceed = [contentwriteToFile:newFielPathatomically:YESencoding:NSUTF8StringEncodingerror:&error];
NSLog(@"isSucceed:%hhd",isSucceed);
NSLog(@"newFielPath:%@",newFielPath);
NSString*documentsPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
//(2)拼接上要存储的文件路径(前面自动加上/),如果没有此文件系统会自动创建一个
NSString*newFielPath1 = [documentsPath1stringByAppendingPathComponent:@"aa.txt"];
//(3)
//读取数据
NSError*error1 =nil;
//(最后也可以&error)
NSString*content1 = [NSStringstringWithContentsOfFile:newFielPath1encoding:NSUTF8StringEncodingerror:nil];
NSLog(@"content1:%@",content1);