int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *string = [[NSMutableString alloc] init];
for (int i=0; i<10; i++){
[string appendString:@"你好\n"];
}
NSError *error;
BOOL success = [string writeToFile:@"/tmp/hello.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (success) {
NSLog(@"done writing /tmp/hello.txt");
} else {
NSLog(@"writing /tmp/hello.txt failed: %@.", error.localizedDescription);
}
}
return 0;
}
将字符串写入文件,指定字符串编码格式,一般为utf8,如果文件写入失败,显示错误信息。