黑马程序员技术交流社区

标题: NSFileManager用法深入一 [打印本页]

作者: 许本亮    时间: 2015-11-11 09:31
标题: NSFileManager用法深入一
/*

NSFileManager用法深入一

用于文件访问:获取文件、文件夹的属性和子目录的信息


*/
#import <Foundation/Foundation.h>

int main() {
    @autoreleasepool {

        NSFileManager *fm = [NSFileManager defaultManager];

        NSString *filepath = @"/Users/apple/Desktop/Block.rtf";

         NSString *dirpath = @"/Users/apple/Desktop";

       //1)获取文件的信息(属性)

        NSDictionary *dict = [fm attributesOfItemAtPath:filepath error:nil];
        NSLog(@"%@",dict);
        //找出文件创建者
        NSLog(@"%@,%@",[dict objectForKey:@"NSFileOwnerAccountName"],dict[@"NSFileOwnerAccountName"]);

        //2)获取指定目录下的文件及子目录
        //使用递归的方式获取当前目录及子目录下的所有的文件及文件夹(耗性能)

      NSArray *subPath = [fm subpathsAtPath:dirpath];

        //不是使用递归的方式获取(常用方式)
        subPath = [fm subpathsOfDirectoryAtPath:dirpath error:nil];

        //获取指定目录下的文件及目录信息(不获取后代路径)
        subPath = [fm contentsOfDirectoryAtPath:dirpath error:nil];


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



      }
    return 0;
}







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2