2. 文件属性和目录遍历用法:NSString *filePath = @"/Users/Mac-leaf/Desktop/arr.plist"; NSString *filePath2 = @"/"; //系统根目录(只读) // 1) 判断文件是否存在 //创建文件管理对象,调用defaultManager 创建一个文件管理的单例对象 //单例对象:在程序运行期间,只有一个对象存在 NSFileManager *fm = [NSFileManager defaultManager]; BOOL isYES = [fm fileExistsAtPath:filePath]; NSLog(@"-->%d",isYES); if(isYES){ BOOL isDir; // 2)判断是否是一个目录
[fm fileExistsAtPath:filePath isDirectory:&isDir]; if (isDir) { NSLog(@"这是一个目录"); }else{ NSLog(@"这不是一个目录"); } } // 3) 判断文件是否可读 isYES = [fm isReadableFileAtPath:filePath]; // 4) 是否可写 isYES = [fm isWritableFileAtPath:filePath2]; // 5) 是否可删除 isYES = [fm isDeletableFileAtPath:filePath2]; NSLog(@"-->%d",isYES);
//创建文件对象 NSFileManager *fm = [NSFileManager defaultManager]; NSString *filePath = @"/Users/Mac-leaf/Desktop/arr.plist"; NSString *dirPath = @"/Users/Mac-leaf/Desktop/arr"; //1)如何获取文件的信息(属性) NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil]; NSLog(@"%@",dict); NSLog(@"%@,%@",[dict objectForKey:@"NSFileOwnerAccountName"],dict[@"NSFileOwnerAccountName"]); //2)获取指定目录下文件及子目录 //使用递归的方式 获取当前目录及子目录下的所有的文件及文件夹 NSArray *subPaths = [fm subpathsAtPath:dirPath]; //subpathsOfDirectoryAtPath 不是使用递归的方式获取的 subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil]; NSLo