zoukankan      html  css  js  c++  java
  • NSFileManager

     

        

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

        NSString *documentPath = [pathArray firstObject];

        

        NSString *filePath = [documentPath stringByAppendingPathComponent:@"test.txt"];

        

        

        // 文件属性 -------------------------

        NSFileManager *fileManager = [NSFileManager defaultManager];

        BOOL flag = [fileManager isWritableFileAtPath:filePath];

        flag = [fileManager isReadableFileAtPath:filePath];

        flag = [fileManager isDeletableFileAtPath:filePath];

        flag = [fileManager isExecutableFileAtPath:filePath];

        NSArray *componentsArray = [fileManager componentsToDisplayForPath:filePath];

        NSLog(@"array = %@", componentsArray);

        NSData *data = [fileManager contentsAtPath:filePath];

        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"str = %@", str);

        NSLog(@"flag = %i", flag);

        

        //创建目录、创建文件、拷贝文件、删除文件 -------------------------

        NSString *dir = [documentPath stringByAppendingPathComponent:@"a/b"];

        BOOL exists = [fileManager fileExistsAtPath:dir];

        if(!exists)

        {

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

        }

        

        // 创建文件

        NSString *ffilePath = [dir stringByAppendingPathComponent:@"test.txt"];

        if(![fileManager fileExistsAtPath:ffilePath isDirectory:NO])

        {

            [fileManager createFileAtPath:ffilePath contents:data attributes:nil];

        }

        NSLog(@"ffilepath = %@", ffilePath);

        // 拷贝文件

        NSString *dictFilePath = [[ffilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"test_copy.txt"];

        [fileManager copyItemAtPath:ffilePath toPath:dictFilePath error:nil];

        // 删除文件

        [fileManager removeItemAtPath:ffilePath error:nil];

        

        // 获取指定目录下的文件和文件夹

        // 1.contentsOfDirectoryAtPath 只遍历当前目录下的,子目录下的文件不遍历

        NSArray *dirArray = [fileManager contentsOfDirectoryAtPath:documentPath error:nil];

        NSLog(@"dirArray = %@", dirArray);

        

        // 2 enumeratorAtPath 获取目录下的所有文件,包括所有子目录

        NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:documentPath];

        NSMutableArray *dirArray2 = [[NSMutableArray alloc]initWithCapacity:1];

        while ([dirEnum nextObject])

        {

            [dirArray2 addObject:[dirEnum nextObject]];

        }

        NSLog(@"dirArray2 = %@", dirArray2);

  • 相关阅读:
    QQ家园熄灭不了解决方法
    那时我们还年轻[转]
    QQ游戏图标熄灭大全
    FlashDevelop快捷键
    linux 全局搜索某一文件并将文件内容并进行替换的命令
    navigate 10.0.5 regist cn
    线程、socket、stl 以及并发设计
    drupal真不错
    网卡问题解决思路linux版
    socket错误:Program received signal SIGPIPE, Broken pipe
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4980923.html
Copyright © 2011-2022 走看看