zoukankan      html  css  js  c++  java
  • iOS获取沙盒路径并写入文件

    .h文件中

    //你需要的数据集合形式
    @property(nonatomic,strong)NSMutableArray *groupArray;//数组
    @property(nonatomic,strong)NSMutableDictionary *allDict;//字典

    //判断沙盒里面是否有需要的对象
    +(ShareDataHandleModel*)shareDataHandleModel;


    //获取沙盒文件中document路径
    +(NSString*)documentPath;


    //通过文件名从沙盒读数据
    -(void)readDataFromDocumentWithFileName:(NSString*)fileName;//数组

    -(void)readDataFromDocumentWithFileNameToDict:(NSString *)fileName;//字典


    //写入
    -(void)myWirteTofile:(NSString*)fileName;

    .m文件中

    +(ShareDataHandleModel*)shareDataHandleModel
    {
       //如果为空 创建一个新的
        if (nil==shareModel) {
                    shareModel=[[ShareDataHandleModel alloc]init];
        }
        return shareModel;
    }


    //获取沙盒文件中document路径
    +(NSString*)documentPath
    {
        NSArray *pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        return pathArray[0];
    }


    //通过文件名从沙盒读数据(数组)
    -(void)readDataFromDocumentWithFileName:(NSString*)fileName
    {
        //拼接文件路径
        NSString *filePath=[[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];
        
        NSArray *tempArray=[NSArray arrayWithContentsOfFile:filePath];
        
        self.groupArray=[NSMutableArray arrayWithObject:tempArray];


    }

    //通过文件名从沙盒读数据(字典)
    -(void)readDataFromDocumentWithFileNameToDict:(NSString *)fileName
    {
        //拼接文件路径
        // NSString *filePath=[[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];
        
        NSString *filePath=[self appendPath:fileName];//使用下面封装好的
        self.allDict=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    }


     

    //封装
    -(NSString*)appendPath:(NSString*)fileName
    {

        //拼接文件路径
        return [[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];

    }


    //写入
    -(void)myWirteTofile:(NSString*)fileName
    {
        NSString *filePath=[self appendPath:fileName];
        
        NSMutableArray *a=self.groupArray[0];
        [a writeToFile:filePath atomically:YES];
        

    }

  • 相关阅读:
    XML 读取器和编写器从URL读取XML
    8月8号 星期五
    080808 晴
    080805
    雨景
    用photoshop批量修改照片(待修改)
    8月7日 晴
    五不
    Android 画渐变的背景
    iOS开发的一些基础知识
  • 原文地址:https://www.cnblogs.com/-ios/p/4672829.html
Copyright © 2011-2022 走看看