1) 当读文件-文件不存在时:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// NSMutableString *str = [[NSMutableString stringWithUTF8String:"I have a apple"] init];
NSMutableString *str = [[NSMutableString alloc] init];
for(int i = 0; i < 10; i++) {
[str appendString:@"I have a apple, Nice One! !\n"];
NSLog(@"the data is %@", str);
NSLog(@"the num is %d",i);
}
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]);
}
// read data from a file:
NSError *errRead;
NSString *readstr = [[NSString alloc] initWithContentsOfFile:@"/Users/xxxx/xxxx/test/coola.txt"
encoding:NSUTF8StringEncoding
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);
}
}
return 0;
}
Result:
2018-03-13 20:19:01.973211+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
2018-03-13 20:19:01.973716+0800 TOCFileHandlinga[60327:3646534] the num is 0
2018-03-13 20:19:01.973759+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.973797+0800 TOCFileHandlinga[60327:3646534] the num is 1
2018-03-13 20:19:01.973817+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.973836+0800 TOCFileHandlinga[60327:3646534] the num is 2
2018-03-13 20:19:01.973847+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.973857+0800 TOCFileHandlinga[60327:3646534] the num is 3
2018-03-13 20:19:01.973915+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.973933+0800 TOCFileHandlinga[60327:3646534] the num is 4
2018-03-13 20:19:01.974385+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.974413+0800 TOCFileHandlinga[60327:3646534] the num is 5
2018-03-13 20:19:01.974489+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.974539+0800 TOCFileHandlinga[60327:3646534] the num is 6
2018-03-13 20:19:01.974553+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.975182+0800 TOCFileHandlinga[60327:3646534] the num is 7
2018-03-13 20:19:01.975289+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.975318+0800 TOCFileHandlinga[60327:3646534] the num is 8
2018-03-13 20:19:01.975330+0800 TOCFileHandlinga[60327:3646534] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:19:01.975341+0800 TOCFileHandlinga[60327:3646534] the num is 9
2018-03-13 20:19:01.977293+0800 TOCFileHandlinga[60327:3646534] Writing file is successful!
2018-03-13 20:19:02.047783+0800 TOCFileHandlinga[60327:3646534] There is no content in file, failed to read: The file “coola.txt” couldn’t be opened because there is no such file.
Program ended with exit code: 0
2)当读文件-正常结束
2018-03-13 20:42:34.000087+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
2018-03-13 20:42:34.000348+0800 TOCFileHandlinga[61623:3734311] the num is 0
2018-03-13 20:42:34.000373+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.000389+0800 TOCFileHandlinga[61623:3734311] the num is 1
2018-03-13 20:42:34.000404+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.000418+0800 TOCFileHandlinga[61623:3734311] the num is 2
2018-03-13 20:42:34.000434+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.000447+0800 TOCFileHandlinga[61623:3734311] the num is 3
2018-03-13 20:42:34.000460+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.000474+0800 TOCFileHandlinga[61623:3734311] the num is 4
2018-03-13 20:42:34.000488+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.994404+0800 TOCFileHandlinga[61623:3734311] the num is 5
2018-03-13 20:42:34.994452+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.994467+0800 TOCFileHandlinga[61623:3734311] the num is 6
2018-03-13 20:42:34.994480+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.994494+0800 TOCFileHandlinga[61623:3734311] the num is 7
2018-03-13 20:42:34.994504+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.995010+0800 TOCFileHandlinga[61623:3734311] the num is 8
2018-03-13 20:42:34.995050+0800 TOCFileHandlinga[61623:3734311] the data is I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
2018-03-13 20:42:34.995085+0800 TOCFileHandlinga[61623:3734311] the num is 9
2018-03-13 20:42:34.997374+0800 TOCFileHandlinga[61623:3734311] Writing file is successful!
2018-03-13 20:42:34.997796+0800 TOCFileHandlinga[61623:3734311] The file's content is as follow: I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
I have a apple, Nice One! !
Program ended with exit code: 0