iOS中文件操作(NSFileManager)

作者: HJXu | 来源:发表于2016-02-02 17:07 被阅读4325次

    iOS的沙盒机制,应用只能访问当前应用目录下的文件内容。应用内所产生的如图像、文件、缓存等内容也都必须存储在自己的沙盒内。
    默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。


    CAC73620-034D-4EDE-9FE2-3AC7C6F22615.png

    其中Library包含Caches、Preferences目录。


    D5F47D62-3B0D-405A-82D9-930B66F57963.png
    (完整路径为:/Users/xxxx/Library/Developer/CoreSimulator/Devices)

    沙盒结构

    Application:存放程序源文件,上架前经过数字签名,上架后不可修改
    Documents:常用目录,iCloud备份目录,存放数据,这里不能存缓存文件,否则上架
    不被通过
    Library:
    &Caches:存放体积大又不需要备份的数据,比如下载的音乐,视频,SDWebImage缓存等
    &Preference:设置目录,iCloud会备份设置信息
    tmp:存放临时文件,不会被备份,而且这个文件下的数据有可能随时被清除的可能
    

    1,如何获取沙盒根路径

    -(void)directoryHome{
    
    NSString *directoryHome=NSHomeDirectory();
    
    NSLog(@"app_home: %@",directoryHome);
    
    }
    

    2,获取Documents目录

    -(NSString *)dirDoc{
    
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSLog(@"app_home_doc: %@",documentsDirectory);
    
    return documentsDirectory;
    }
    

    3,获取Library目录路径:

    //获取Library目录
    -(void)dirLib{
    
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    
    NSString *libraryDirectory = [paths objectAtIndex:0];
    
    NSLog(@"app_home_lib: %@",libraryDirectory);
    
    }```
    
    4,获取Cache目录路径:
    
    

    //获取Cache目录
    -(void)dirCache{

    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString *cachePath = [cacPath objectAtIndex:0];

    NSLog(@"app_home_lib_cache: %@",cachePath);

    }```

    5,获取Tmp目录路径:

    //获取Tmp目录
    -(void)dirTmp{
    
    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
    
    NSString *tmpDirectory = NSTemporaryDirectory();
    
    NSLog(@"app_home_tmp: %@",tmpDirectory);
    
    }```
    
    6,创建文件夹:
    
    

    //创建文件夹
    -(void *)createDir{

    NSString *documentsPath =[self dirDoc];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];

    }

    创建目录

    BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];`

    if (res) {

    NSLog(@"文件夹创建成功");

    }else

    NSLog(@"文件夹创建失败");

    }```

    7,创建文件

    //创建文件
    -(void *)createFile{
    
    NSString *documentsPath =[self dirDoc];
    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    
    BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
    
    if (res) {
    
    NSLog(@"文件创建成功: %@" ,testPath);
    
    }else
    
    NSLog(@"文件创建失败");
    
    }```
    
    8,写数据到文件
    
    

    //写文件
    -(void)writeFile{

    NSString *documentsPath =[self dirDoc];

    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];

    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];

    NSString *content=@"测试写入内容!";

    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    if (res) {

    NSLog(@"文件写入成功");

    }else

    NSLog(@"文件写入失败");

    }```

    9,读文件数据:

    //读文件
    -(void)readFile{
    
    NSString *documentsPath =[self dirDoc];
    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    
    //    NSData *data = [NSData dataWithContentsOfFile:testPath];
    
    //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    
    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"文件读取成功: %@",content);
    
    }```
    
    10,文件属性:
    
    

    //文件属性
    -(void)fileAttriutes{

    NSString *documentsPath =[self dirDoc];

    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];

    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];

    NSArray *keys;

    id key, value;

    keys = [fileAttributes allKeys];

    int count = [keys count];

    for (int i = 0; i < count; i++)

    {

    key = [keys objectAtIndex: i];

    value = [fileAttributes objectForKey: key];

    NSLog (@"Key: %@ for value: %@", key, value);

    }

    }```

    11,删除文件

    //删除文件
    -(void)deleteFile{
    
    NSString *documentsPath =[self dirDoc];
    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    
    if (res) {
    
    NSLog(@"文件删除成功");
    
    }else
    
    NSLog(@"文件删除失败");
    
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
    
    }
    

    相关文章

      网友评论

      本文标题:iOS中文件操作(NSFileManager)

      本文链接:https://www.haomeiwen.com/subject/axtekttx.html