zoukankan      html  css  js  c++  java
  • iOS 简单的文件写入

    //访问沙盒路径
        /*
        //1.Home主目录(沙盒主目录:里面有Documents,Library,tmp 和一个应用程序)
        NSLog(@"%@",NSHomeDirectory());
        
        //2.Documents
        NSString *DocumentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        NSLog(@"Documents:%@",DocumentsPath);
        
        //3.Library
        NSString *LibraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
        NSLog(@"Library:%@",LibraryPath);
        
        
        //4.temp
        NSLog(@"tmp:%@",NSTemporaryDirectory());
        
        
        //5.Caches(Library下面的)
        NSString *cachesPath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSLog(@"caches:%@",cachesPath);
        
        //6.User
        NSString *user=NSUserName();
        NSLog(@"user:%@",user);
        
        //7.NSBundle
        NSString *bundle=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];
        NSLog(@"bundle:%@",bundle);
        */
        
        
        
        
        
    //简单文件写入
        //NSString写入
         //1.写入路径(往哪里写)
        NSString *DocumentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSLog(@"%@",DocumentsPath);
         //2.拼接文件路径
        NSString *filePath=[DocumentsPath stringByAppendingString:@"/myText.txt"];
         //3.准备写入的内容
        NSString *content=@"hello world";
         //4.写入 atomically:原子性 yes:全写 no:能写多少写多少
        [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        
        //NSAyyay
        //1.写入路径(同上)
        
        //2.拼接文件路径
        NSString *arrayFile=[DocumentsPath stringByAppendingString:@"/array.plist"];
        
        //3.准备写入内容
        NSArray *array=@[@"123",@"456",@"789"];
        
        //4.写入
        [array writeToFile:arrayFile atomically:YES];
        
        
        
        //NSDictionary
        
        //1.写入路径(同上)
        
        //2.拼接文件路径
        NSString *dictFile=[DocumentsPath stringByAppendingString:@"/dic.plist"];
        
        //3.准备写入内容
        NSDictionary *dict=@{@"1":@"a",@"2":@"b",@"3":@"c"};
        
        //4.写入
        [dict writeToFile:dictFile atomically:YES];
        
        //5.读取
        //NSString
        NSString *readString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",readString);
        
        
        //NSArray
        NSArray *readArray=[NSArray arrayWithContentsOfFile:arrayFile];
        NSLog(@"%@",readArray);
        
        
        //NSDictionary
        NSDictionary *readDict=[NSDictionary dictionaryWithContentsOfFile:dictFile];
        NSLog(@"%@",readDict);
        
        
        
        

  • 相关阅读:
    自动驾驶技术其实是一个美丽的谎言
    自动驾驶技术其实是一个美丽的谎言
    Shoutem旨在成为React Native移动应用领域的WordPress
    Mongodb安装
    spring mvc记录各个controller访问开始结束时间,以及耗时时间 线程安全
    python 升级
    SpringMVC的拦截器Interceptor
    使用org.apache.tools.zip实现zip压缩和解压
    使用 ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体
    Python爬虫入门教程 4-100 美空网未登录图片爬取
  • 原文地址:https://www.cnblogs.com/-ios/p/4672920.html
Copyright © 2011-2022 走看看