// 写文件
NSMutableString *str = [[NSMutableString alloc] init];
for(int i = 0; i < 10; i++)
{
[str appendString:@"I have a apple, Nice One! !\n"];
}
NSError *err;
BOOL success = [str writeToFile:@"/Users/xxxx/xxxx/test/cool.txt"
atomically:YES
encoding:NSUTF8StringEncoding
error:&err];
if(success)
{
NSLog(@"Writing file is successful!");
}
else
{
NSLog(@"Write file is failed, with error : %@", [err localizedDescription]);
}
// 读文件
NSError *errRead;
//编码格式自己去查
NSString *readstr = [[NSString alloc] initWithContentsOfFile:strName encoding:NSASCIIStringEncoding error:&errRead];
if(!readstr)
{
NSLog(@"There is no content in file, failed to read: %@", [errRead localizedDescription]);
}
else
{
NSLog(@"The file's content is as follow: %@", readstr);
}