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);

  • 相关阅读:
    为什么你SQL Server的数据库文件的Date modified没有变化呢?
    SQL Server中SELECT会真的阻塞SELECT吗?
    ORACLE从共享池删除指定SQL的执行计划
    flink DataStream API使用及原理
    漫谈九品中正制和现阶段阶层分层
    flink dataset api使用及原理
    从flink-example分析flink组件(3)WordCount 流式实战及源码分析
    TODO supply a title
    avalon2学习教程01
    avalon1与avalon2的异同点
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4980923.html
Copyright © 2011-2022 走看看