iOS经典讲解之获取沙盒文件路径写入和读取简单对象

  1. #import "RootViewController.h"  
  2.   
  3. @interface RootViewController ()  
  4.   
  5. @end  
  6.   
  7. 实现文件:  
  8. @implementation RootViewController  
  9.   
  10. - (void)viewDidLoad {  
  11.     [super viewDidLoad];  
  12.   
  13.     [self path];  
  14.     [self writeFile];  
  15.     [self readingFile];  
  16.       
  17. }  
  18.   
  19. // 打印沙盒中的文件夹路径  
  20. - (void)path  
  21. {  
  22.     // 每运行一次 相当于重新安装一次 重新安装就重新分配一个沙盒 所以你每次运行路径都不一样  
  23.     //NSDocumentDirectory要打印文件夹地址  
  24.     // NSUserDomainMask 搜索范围  
  25.       
  26.       
  27.     //documents 路径  
  28.     // 该文件夹 一般存储用户的一些数据  
  29.     NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  30.     NSString *document = [documentsPathArr lastObject];  
  31.     NSLog(@"%@", document);  
  32.       
  33.   // Caches缓存文件夹路径  
  34.  // 该文件夹 一般存储缓存文件  
  35.    NSArray *cachesPathArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  36.       
  37.     NSString *cachesPath = cachesPathArr[0];  
  38.     NSLog(@"%@", cachesPath);  
  39.       
  40.     // 打印tmp文件夹  
  41.     // 该文件夹 一般存储临时文件  
  42.     NSString *tempPath = NSTemporaryDirectory();  
  43.     NSLog(@"%@", tempPath);  
  44.       
  45.     // 打印沙盒的主目录路径  
  46.     NSLog(@"%@", NSHomeDirectory());     
  47. }  
  48.   
  49. // 简单对象引入文件  
  50. // 注意如果你写入字典或者数组 那么字典数组中存储的必须是简单对象 无法写入复杂对象  
  51. // 复杂对象  
  52. //自定义的类 比如Person类  
  53. - (void)writeFile  
  54. {  
  55.     //简单对象  
  56.     // 字符串 字典 数组 data...系统提供的类  
  57.     // 写入文件的路径  
  58.     // 在documents路径下写入xiaoshuo.txt  
  59.     NSString *str = @"第一章 ";  
  60.     NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  61.     NSString *documentsPath = [documentsPathArr lastObject];  
  62.     // 拼接要写入文件的路径  
  63.     NSString *path = [documentsPath stringByAppendingPathComponent:@"xiaoshuo.txt"]; // stringByAppendingString:@"/xiaoshuo.txt" 也可以  
  64.     //atomically 如果YES在你写入的过程中 出现程序的崩溃不影响写入  
  65.     [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];  
  66.     NSLog(@"%@", path);  
  67.       
  68.     // 简单对象写入步骤  
  69.     // 1、拼写要写入的路径 (路径一定要拼对)  
  70.     // 2、调用写入方法 完事  
  71.       
  72.     // 写入一个数组 shuzu.txt  
  73.     // 必须给后缀类型 你不给那 就默认是txt格式的,如果是txt格式里面是xml结构,如果是plist格式里面就是plist结构  
  74.     NSString *arrPath = [documentsPath stringByAppendingPathComponent:@"shuzu.plist"];  
  75.     NSArray *array = @[@"八戒"@"悟空"@"三藏"@"悟净"];  
  76.     // 调用写入方法  
  77.     [array writeToFile:arrPath atomically:YES];  
  78.     NSLog(@"%@", arrPath);  
  79.       
  80.     // 写入一个字典zidian.plist  
  81.     NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];  
  82.     NSDictionary *dic = @{@"2":@""};  
  83.     [dic writeToFile:dicPath atomically:YES];  
  84.     NSLog(@"%@", dicPath);  
  85.       
  86.     // data的写入 后缀.da  
  87.       
  88.     NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];  
  89.     NSString *dataStr = @"你猜我是谁";  
  90.     NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];  
  91.     // 写入文件  
  92.     [data writeToFile:dataPath atomically:YES];  
  93.     NSLog(@"%@", dataPath);  
  94.     
  95. }  
  96.   
  97. // 读取写入的文件  
  98.   
  99. - (void)readingFile  
  100. {  
  101.     // 读字符串  
  102.     // 获取路径  
  103.     NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  104.     NSString *documentsPath = [documentsPathArr lastObject];  
  105.     // 拼接要写入文件的路径  
  106.      NSString *path = [documentsPath stringByAppendingPathComponent:@"xiaoshuo.txt"];  
  107.     // 从路径中读取字符串  
  108.     NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];  
  109.     NSLog(@"%@", str);      
  110.     // 读数组文件  
  111.     // 获取路径  
  112.     NSString *arrPath = [documentsPath stringByAppendingPathComponent:@"shuzu.plist"];  
  113.     NSArray *array = [NSArray arrayWithContentsOfFile:arrPath];  
  114.     NSLog(@"%@", array);  
  115.       
  116.     // 读取字典  
  117.     // 获取路径  
  118.     NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];  
  119.     NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];  
  120.     NSLog(@"%@", dic);  
  121.       
  122.     //读data  
  123.  NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];  
  124.     // 读取data  
  125.     NSData *data = [NSData dataWithContentsOfFile:dataPath];  
  126.     // 将data转化成字符串  
  127.     NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  128.     NSLog(@"%@", dataStr);  
  129. }  
  130.   
  131. @end  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值