zoukankan      html  css  js  c++  java
  • 初级数据持久化(沙盒)

      1 - (void)viewDidLoad {
      2     [super viewDidLoad];
      3     // Do any additional setup after loading the view.
      4     self.view.backgroundColor = [UIColor whiteColor];
      5     
      6     
      7     //沙盒:系统为每个应用程序分配的一个文件夹,文件夹名字随机,别人不能访问
      8     
      9     NSString *sandBoxPath = NSHomeDirectory() ;
     10     NSLog(@"%@",sandBoxPath);
     11     
     12     //沙盒文件下有3个文件夹,分别是 Documents Library tmp
     13     
     14     /*
     15      1.Documents 该文件下的内容会上传到iCloud ,iCloud给每个用户分配空间5G,一般用来存储一些用户信息,游戏进度信息
     16      
     17      2.获取Documents 文件夹路劲
     18      
     19      */
     20     
     21     /**
     22      *  <#Description#>
     23      *
     24      *  @param NSDocumentDirectory 搜索哪个文件夹的路径
     25      *  @param NSUserDomainMask    在那个文件系统中搜索
     26      *  @param YES                 要获取绝对路径(YES)相对路径(NO)
     27      *
     28      *  @return
     29      */
     30     
     31     NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     32     
     33 //    NSLog(@"%@",documentArray);
     34     
     35     //数组就有一个元素
     36     NSString *myDocPath = [documentArray firstObject];
     37     
     38 //    NSLog(@"%@",myDocPath);
     39     
     40     
     41     
     42     //Library 文件夹下有两个子文件 Caches(缓存) Preferences
     43     
     44     //Library 文件夹路径
     45     NSArray *libraryArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
     46     
     47     NSString *libraryPath = libraryArray[0];
     48     
     49 //    NSLog(@"%@",libraryPath);
     50     
     51     
     52     
     53     //获取Caches文件夹的路径 一般放一些缓存文件 , 网络缓存等等
     54     
     55     NSArray *cachesArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) ;
     56     
     57     NSString *cacherPath = [cachesArray lastObject];
     58     
     59 //    NSLog(@"%@",cacherPath);
     60 
     61     
     62     //获取Preferences文件夹路径
     63     
     64 //    NSArray *preferencesArray = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES) ;
     65 //    
     66 //    NSString *preferencesPath1 = [preferencesArray lastObject];
     67     
     68     NSString *preferencesPath = [sandBoxPath stringByAppendingString:@"/Library/Preferences"] ;
     69      NSString *preferencesPath1 = [libraryPath stringByAppendingPathComponent:@"Preferences"] ;
     70     
     71 //    NSLog(@"%@",preferencesPath);
     72     
     73     
     74 
     75     //获取tem文件夹的路径(临时文件,比如下载完之后的压缩包,解压完之后就删除)
     76     
     77     //获取tmp目录
     78     
     79     //获取应用程序的tmp目录要比获取Documents目录容易的多。使用函数NSTemporaryDirectory ()可以获得tmp目录路径。
     80     
     81     NSString* tempPath = NSTemporaryDirectory();
     82     
     83     //获取文件的完整路径。
     84     
     85     NSString* tempFile = [tempPath stringByAppendingPathComponent:@"properties.plist"];
     86     
     87     NSLog(@"%@",tempPath);
     88 //
     89     
     90     //.app IOS8 之前存在沙盒文件里面
     91     //当前的应用程序的可执行文件,也就是NSBundle ,在程序编译的过程中,会把自己的所有的文件都打包进NSBundle中,这些文件都是只读的
     92     
     93     //获取.app文件的路径
     94     NSString *appPath = [[NSBundle mainBundle]resourcePath];
     95 //    NSLog(@"*********%@",appPath);
     96     
     97     //获取.app某个文件的路径
     98     NSString *infoPlistPath = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];
     99     NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:infoPlistPath];
    100     
    101     NSLog(@"%@",dic);
    102     
    103     
    104     
    105     //读写简单文件 NSString NSDictionary NSArray NSData 可以直接写入本地
    106     
    107     //将字符串写入本地
    108     /*
    109      1.首先需要知道文件(字符串)写进哪里,也就是路径
    110      2.需要知道写入的哪个文件
    111      3.字符串调用写入的方法
    112      */
    113     
    114     //文件写入的路径(后面拼接的是文件的名字),但此时该路径下还没有文件
    115     //后面写的会覆盖之前的
    116     
    117     NSString *filePath = [myDocPath stringByAppendingPathComponent:@"文档.txt"];
    118     
    119 //    NSString *fiel = @"什么鬼";
    120     
    121     NSString *fiel1 = @"";
    122     
    123     /*
    124      fiel  写入的文件
    125      参数1:路径
    126      参数2:是否需要保护正在写入的文件
    127      参数3:编码格式
    128      参数4:错误信息
    129      */
    130     
    131     NSError *error = nil ;
    132     [fiel1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    133     
    134 //    NSLog(@"%@",filePath);
    135     
    136     
    137     //用过路径读取文件
    138     
    139     NSString *readFiel = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    140     
    141     NSLog(@"%@",readFiel);
    142     
    143     
    144     
    145     //读写数组
    146     
    147     NSString *arrFielPath = [cacherPath stringByAppendingPathComponent:@"arr.plist"] ;
    148     
    149     NSArray *arrFiel = @[@"123",@"111",@"5458444",@"9999",@"4444"];
    150     
    151     [arrFiel writeToFile:arrFielPath atomically:YES];
    152     
    153 //    NSLog(@"%@",arrFielPath);
    154     
    155     //本地读取数组
    156     NSArray *readarr = [[NSArray alloc]initWithContentsOfFile:arrFielPath];
    157     NSLog(@"%@",readarr);
    158     
    159     
    160     //读写字典
    161     NSString *dicFielPath = [preferencesPath1 stringByAppendingPathComponent:@"preferen.plist"];
    162     
    163     NSDictionary *dic1 = @{@"A2":@"21",@"B3":@"22",@"C4":@"23"};
    164     
    165     [dic1 writeToFile:dicFielPath atomically:YES];
    166     
    167 //    NSLog(@"%@",dicFielPath);
    168     
    169     NSDictionary *dic2 = [[NSDictionary alloc]initWithContentsOfFile:dicFielPath];
    170     NSLog(@"%@",dic2);
    171 
    172 }
  • 相关阅读:
    Fiddler 教程
    Mongodb for C# 分组查询
    C# CryptoStream
    ECharts 纯Javascript图表库
    Mongodb For C# "Query" 对象常用的方法
    WPF 获取指定文件的Icon
    SymbolSource
    ubuntu下安装Docker
    老李推荐:第1章2节《MonkeyRunner源码剖析》概述:边界
    老李推荐: 第1章1节《MonkeyRunner源码剖析》概述:前言
  • 原文地址:https://www.cnblogs.com/yyxblogs/p/4878852.html
Copyright © 2011-2022 走看看