• IOS 沙盒路径的机制


    1.前往沙盒目录:在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/  前往

    2.沙盒结构:每个沙盒含有3个文件夹:Documents, Library 和 tmp。

    Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
    Library:存储程序的默认设置或其它状态信息;

    Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

    tmp:提供一个即时创建临时文件的地方。

    //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放  
              NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
              NSString *documentsPath = [docPath objectAtIndex:0];  
              NSLog(@"Documents目录:%@",documentsPath); 


    3.下面开始向目录里面创建文件,然后向文件里面写入内容:

     方法一:

    复制代码
     1     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
     2         NSString *documentsPath = [docPath objectAtIndex:0];  
     3         //写入文件  
     4         if (!documentsPath) {  
     5             NSLog(@"目录未找到");  
     6         }else {  
     7             NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
     8             NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];  
     9             [array writeToFile:filePaht atomically:YES];  
    10         }

     方法二:

        

        // 保存有关用户头像的背景图文件的路径

        #define USER_BACKGROUND_PATH [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserBackground"]

        //创建沙盒路径

        [NSFileManager createFileFolder: USER_BACKGROUND_PATH];

       //把图片写入沙盒路径中

        NSString *filePath = nil;

        filePath = [NSString stringWithFormat: @"%@/%@.jpg", USER_BACKGROUND_PATH, fileName];

         [imageJPEGData writeToFile:filePath atomically:YES]

    4.接下来是把该文件中的内容读出来:

     方法一:

    1     //读取文件  
    2     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    3         NSString *documentsPath = [docPath objectAtIndex:0];  
    4         NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
    5         NSArray *fileContent = [[NSArrayalloc] initWithContentsOfFile:readPath];  
    6         NSLog(@"文件内容:%@",fileContent); 

    方法二:

    // 获取图片的路径

    + (NSString *)getBackgroundFilePath:(NSString *)fileName

    {

        if (fileName == nil) {

            return nil;

        }

        return [NSString stringWithFormat: @"%@/%@.jpg", USER_BACKGROUND_PATH, fileName];

    }

     参考:http://www.cnblogs.com/taintain1984/archive/2013/03/19/2969201.html

  • 相关阅读:
    三十岁了是否该改行——我完全晕倒了
    asp.net Context.Handler 页面间传值的几种方法
    信道与信道栈
    正则表达式之字符组
    移动Web界面样式CSS3
    Media Queries语法总结
    WCF全面解析(上下册)
    HTML5移动Web开发指南
    HTML5移动开发即学即用(双色)
    可靠会话最佳实践
  • 原文地址:https://www.cnblogs.com/yuyu-2012/p/5120765.html
Copyright © 2020-2023  润新知