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];
        

    }

  • 相关阅读:
    027、获取后台正在运行的程序
    026、TelephonyManager的应用
    025、WiFi服务
    024、Wallpaper桌面墙纸
    023、在手机上实现打开文件功能
    Git使用ssh协议配置Github远程仓库避免踩坑指南(Windows环境)
    Linxu网络常用命令(CentOS 7)
    插入耳机后,内置麦克风(话筒)输入音量变很轻的解决办法(Windows 10 + Conexant声卡)
    PowerShell Write-Output 支持参数数组传入
    工商银行网银助手无法安装:系统无法打开指定的设备或文件
  • 原文地址:https://www.cnblogs.com/-ios/p/4672829.html
Copyright © 2011-2022 走看看