Objective-C学习笔记-使用NSString与NSData读写文件
生活随笔
收集整理的這篇文章主要介紹了
Objective-C学习笔记-使用NSString与NSData读写文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.NSString讀寫文件
NSString *helloText=@"您好啊!";NSError *error;if ([helloText writeToFile:@"/tmp/test.txt" atomically:true encoding:NSUTF8StringEncoding error:&error]){NSLog(@"writeToFile success");}else{NSLog(@"writeToFile failed:%@",[error localizedDescription]);}NSString *readText=[[NSString alloc] initWithContentsOfFile:@"/tmp/test.txt" encoding:NSUTF8StringEncoding error:NULL];NSLog(@"%@",readText);2.NSData讀寫文件
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];NSURLRequest *request=[NSURLRequest requestWithURL:url];NSError *error;NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];if (!data){NSLog(@"fetch failed with error %@",[error localizedDescription]);}else{if ([data writeToFile:@"/tmp/test1.txt"options:NSDataWritingAtomicerror:&error]){NSLog(@"write success");}else{NSLog(@"write failed with error %@",[error localizedDescription]);}}NSData *readData=[NSData dataWithContentsOfFile:@"/tmp/test1.txt"];NSLog(@"file size is %ld",[readData length]);轉載于:https://www.cnblogs.com/Potato-Eater/p/9693850.html
總結
以上是生活随笔為你收集整理的Objective-C学习笔记-使用NSString与NSData读写文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统的中断和异常
- 下一篇: BFC与合并 浅析