NSFileManager基本操作

NSFileManager中包含了用来查询单词库目录、创建、重命名、删除目录以及获取/设置文件属性的方法(可读性,可编写性等等)。

每个程序都会有它自己的沙盒,通过它你可以阅读/编写文件。

 

  •  NSError *error;//保存错误信息
  •    //创建文件管理器

        NSFileManager *fileManager = [NSFileManager defaultManager];

        //指向文件目录

        NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    //指向文件目录的另一种方法

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

    //    NSLog(@"%d",[paths count]);//=1

    //  NSString *documentsDirectory = [paths objectAtIndex:0];

        NSLog(@"%@",documentsDirectory);

        


    //创建一个目录

        NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];

        [[NSFileManager defaultManager]createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];

        

        //创建一个文件

        NSString *filePath = [directoryPath stringByAppendingPathComponent:@"file.txt"];

        [[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];

        

        //需要写入的字符串

        NSString *str = @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com你好";

        //写入文件(文件目录必须提前创建,文件可以不必提前创建,写入文件的时候,如果没有文件会自动创建)

        [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

        //显示文件目录的内容

        NSLog(@"文件目录内容:%@",[fileManager contentsOfDirectoryAtPath:directoryPath error:&error]);

       

        //对一个文件重命名(通过移动文件实现)

        NSString *filePath1 = [directoryPath stringByAppendingPathComponent:@"file1.txt"];

        if([fileManager moveItemAtPath:filePath toPath:filePath1 error:&error]!=YES)

            NSLog(@"unable to move file:%@",[error localizedDescription]);

        

       

        

        //删除一个文件

        if([fileManager removeItemAtPath:filePath1 error:&error]!=YES)

            NSLog(@"unable to remove file:%@",[error localizedDescription]);

        

       

        

        //获取应用程序Documents文件夹里的文件及文件夹列表

        //fileList便是Documents文件夹下所有文件的文件名及文件夹名的数组

        NSArray *fileList = [[NSArray alloc]init];

        fileList = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];

        NSLog(@"Documents:%d,%@",[fileList count],[fileList objectAtIndex:1]);

        

       

    //获取某个文件夹下所有子文件夹名

        NSMutableArray *dirArray = [[NSMutableArray alloc]init];

        BOOL isDir = NO;

        for(NSString *fileName in fileList)

        {

            NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];

            [fileManager fileExistsAtPath:path isDirectory:&isDir];

            if(isDir)

            {

                [dirArray addObject:fileName];

            }

            isDir = NO;

        }

        NSLog(@"All folders in documents:%@",dirArray);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OC统计load的耗时主要是通过计算开始加载数据的时间和加载完成的时间之间的差值来得到的。在OC中,我们可以使用NSFileManagerNSURLSession等类来实现load的操作并计算其耗时。 首先,我们可以使用NSFileManager的`defaultManager`方法来获取一个文件管理器实例。然后,使用文件管理器的`fileExistsAtPath:`方法可以判断要加载的文件是否存在。这个方法返回一个布尔值,若为真则文件存在,否则不存在。 接下来,我们可以使用NSURLSession的相关类和方法来加载数据。通过创建一个NSURLSession实例,并使用NSURLSessionDataTask的`dataTaskWithURL:completionHandler:`方法来创建一个数据任务,该任务用于从指定的URL地址异步加载数据。然后,可以使用`resume`方法来开始执行任务。当任务完成后,会调用completionHandler中的回调函数,在回调函数内部可以计算加载耗时。 在回调函数中,我们可以使用NSDate类来记录加载完成的时间,并用该时间减去开始加载的时间,就可以得到加载耗时。可以使用NSDate的`date`方法来获取当前时间,然后使用`timeIntervalSinceDate:`方法计算时间差。 最后,我们可以将加载耗时以统计数据的方式保存或显示出来。可以使用NSLog来输出到控制台,也可以使用UILabel或其他UI组件来在界面上展示。 综上所述,以上是使用OC统计load的耗时的基本步骤。具体实现还需根据具体的场景和需求来确定具体的代码逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值