• NSSearchPathForDirectoriesInDomains用法


     

    iOS可操作目录有两种:Documents下与Caches下, 如图:

     

    iOS对于目录的操作

    iOS对于目录的操作

     

     

    1、创建目录createDirectoryAtPath:withIntermediateDirectories:attributes:error:

    NSFileManager * fileManager = nil;

     

    NSArray *paths = nil;

     

    NSString *documentsDirectory = nil;

     

    NSString * folerName = @"Photos";

     

    NSString * fileName = @"myphoto.png";

     

    NSString  * filePath = nil;

     

    UIImage *photoimage = nil;

     

    NSData * imageData = nil;

     

     

     

    //Documents:

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMaskYES);

     

     

    //Caches:

     

     

    paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

     


     

    documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:folerName];

     

    fileManager = [[NSFileManager alloc]init];

     

    [fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:error];

     

     

     

     

     

     

    2、创建目录并在目录中存储对象createFileAtPath: contents: attributes

     

     

     

    filePath = [documentsDirectory stringByAppendingPathComponent:filename];

     

     

     

    if((![fileManager fileExistsAtPath: fullPathToFile])) 

     

         {

     

    photoimage = [[UIImage alloc] imageNamed:@"photo.png"];

     

    imageData = UIImagePNGRepresentation(photoimage);

     

    [fileManager createFileAtPath:filePath contents: imageData attributes:nil];

     

        }

     

    [photoimage release];

     

     

    [fileManager release];

     

     

     

     

    3、删除目录中指定文件

     

     

     

    NSString * filePath = [documentsDirectory stringByAppendingPathComponent: fileName];

     

    [filemanager removeItemAtPath: filePath error:NULL];
     

     

     

    4.

     

    iPhone会为每一个应用程序生成一个私有目录,这个目录位于:

    /Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,

    并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。


    所以通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过:

    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到。

    代码如下:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSLog(@"path:   %@",path);

        打印结果如下:

        path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents

        

    而通过 NSHomeDirectory()也可以得到程序的目录,代码如下:

        NSString *destPath = NSHomeDirectory();

        NSLog(@"path:   %@",destPath);

        打印结果如下:

        path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7

     

    5

    2.

     

    在CODE上查看代码片派生到我的代码片

    1.  (NSString *)dataFilePath {  
    2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    3. NSString *documentsDirectory = [paths objectAtIndex:0];  
    4. return [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"];  
    5. }  
    6. NSFileManager* fm=[NSFileManager defaultManager];  
    7. if(![fm fileExistsAtPath:[self dataFilePath]]){  
    8. //下面是对该文件进行制定路径的保存  
    9. [fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];  
    10. //取得一个目录下得所有文件名  
    11. NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];  
    12. //读取某个文件  
    13. NSData *data = [fm contentsAtPath:[self dataFilePath]];  
    14. //或者  
    15. NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];  
    16. }  


    3.

     

    因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:

    • Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
    • tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
    • Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

    在Documents目录下创建文件

    代码如下:

    1. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory  
    2.                                             , NSUserDomainMask   
    3.                                             , YES);   
    4. NSLog(@"Get document path: %@",[paths objectAtIndex:0]);  
    5.   
    6. NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];   
    7. NSString *content=@"a";   
    8. NSData *contentData=[content dataUsingEncoding:NSASCIIStringEncoding];   
    9. if ([contentData writeToFile:fileName atomically:YES]) {  
    10.     NSLog(@">>write ok.");   
    11. }  


    可以通过ssh登录设备看到Documents目录下生成了该文件。

     

    上述是创建ascii编码文本文件,如果要带汉字,比如:

    1. NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];   
    2. NSString *content=@"更深夜静人已息";   
    3. NSData *contentData=[content dataUsingEncoding:NSUnicodeStringEncoding];   
    4. if ([contentData writeToFile:fileName atomically:YES]) {  
    5.     NSLog(@">>write ok.");   
    6. }  


    如果还用ascii编码,将不会生成文件。这里使用NSUnicodeStringEncoding就可以了。

    通过filezilla下载到创建的文件打开,中文没有问题:

    image

    在其他目录下创建文件

    如果要指定其他文件目录,比如Caches目录,需要更换目录工厂常量,上面代码其他的可不变:

     

    1. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory  
    2.                                                 , NSUserDomainMask   
    3.                                                 , YES);  


    使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。

    tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录:

    NSHomeDirectory()

    也就是Documents的上级目录,当然也是tmp目录的上级目录。那么文件路径可以这样写:

    NSString *fileName=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/myFile.txt"];

    或者,更直接一点,可以用这个函数:

    NSTemporaryDirectory()

    不过生成的路径将可能是:

    …/tmp/-Tmp-/myFile.txt

    使用资源文件

    在编写应用项目的时候,常常会使用资源文件,比如:

    image

    安装到设备上后,是在app目录下的:

    image

    以下代码演示如何获取到文件并打印文件内容:

    1. NSString *myFilePath = [[NSBundle mainBundle]   
    2.                         pathForResource:@"f"   
    3.                         ofType:@"txt"];   
    4. NSString *myFileContent=[NSString stringWithContentsOfFile:myFilePath encoding:NSUTF8StringEncoding error:nil];   
    5. NSLog(@"bundel file path: %@  file content:%@",myFilePath,myFileContent);  


    代码运行效果:

    image


  • 相关阅读:
    GitHub 上的优质开源游戏项目,每个都很厉害
    使用entities html 转义
    TDesign 正式开源发布,腾讯设计云布局重要能力拼图!
    copytoclipboard
    劲爆!Java 18 正式发布,默认 UTF8,finalize 被弃用。。别再乱用了!
    请求合并的 3 种方式,大大提高接口性能。。。
    面试官:说下你公司系统怎么设计防重复数据的?架构师必问!
    别再写垃圾代码了:试试阿里巴巴 Java 开发插件,打造你的团队专属风格。。。
    给你 2 万条数据,怎么快速导入到 MySQL?写得太好了...
    qt6的安装
  • 原文地址:https://www.cnblogs.com/conanwin/p/4250426.html
Copyright © 2020-2023  润新知