读取文件内容的三种方式:
NSString *path = @"/Users/xiaogao/Desktop/path3";
NSString *error;
NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if(error == nil){
NSLog(@"读取文件成功:%@",str1);
}else{
NSLog(@"读取文件失败:%@",error);
}
NSURL *url = [NSURL URLWithString:@"file:///Users/xiaogao/Desktop/path3"];
NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str2);
NSURL *url2 = [NSURL URLWithString:@"http://wwwibaidu.com"];
NSString *str3 = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str3);
写入文件:
NSString *str = @"123456";
NSString *path = @"/Users/xiaogao/Desktop/path4";
NSError *error;
//YES代表要进行原子性操作,也就是会创建一个中间的临时文件
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(error){
//显示主要信息
NSLog(@"写入失败:%@",[error localizedDescription]);
}else{
NSLog(@"写入成功");
}