iOS开发知识体系——File Manager
一、获取文件属性的方法:
1> NSDictionary *folderAttr = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
获取文件夹大小:[folderAttr objectForKey:NSFileSystemSize];
剩余空间:[folderAttr objectForKey:NSFileSystemFreeSize];
代替方法:fileSystemAttributesAtPath
2> NSDictionary * fileAttributes = [FileDefaultManager attributesOfItemAtPath:filePath error:&error];
获取文件大小:unsigned long long length = [fileAttributes fileSize];
代替方法:fileAttributesAtPath:traverseLink
二、常用方法整理
//返回文件大小(字节byte) + (unsigned long long)getFileSize:(NSString *)filePath{ if (![FileDefaultManager fileExistsAtPath:filePath]) {MyNSLog(@"要计算大小的文件不存在!!!filePath=%@", filePath);return 0;} NSError *error=nil;//局部变量必须赋值,并不是默认的nil NSDictionary * fileAttributes = [FileDefaultManager attributesOfItemAtPath:filePath error:&error]; if (fileAttributes == nil || error!=nil) {MyNSLog(@"文件大小计算出错!err=%@",error.localizedDescription);return 0;} return [fileAttributes fileSize]; } //拷贝文件 + (BOOL)fileCopy:(NSString *)sName TargetName:(NSString *)tName{ NSError *error=nil; BOOL isSucces=NO; if (![FileDefaultManager fileExistsAtPath:sName]) { MyNSLog(@"要拷贝的源文件不存在!!!sName=%@", sName); return NO; } if ([FileDefaultManager fileExistsAtPath:tName]) { MyNSLog(@"要拷贝的目标文件已经存在!!!马上删除之...%@", tName); isSucces=[self fileDelete:tName];//如果不删除,后面的拷贝要失败 } isSucces=[FileDefaultManager copyItemAtPath:sName toPath:tName error:&error];//调用拷贝方法 if (error != nil) { MyNSLog(@"拷贝文件出错:%@", [error localizedDescription]); }else{ MyNSLog(@"拷贝文件成功!"); } return isSucces; } //删除文件和文件夹 + (BOOL)fileDelete:(NSString *)filePath{ NSError *error=nil; BOOL isSucces=NO; if (![FileDefaultManager fileExistsAtPath:filePath]) { MyNSLog(@"要删除的地址不存在!!!filePath=%@", filePath); return NO; } isSucces=[FileDefaultManager removeItemAtPath:filePath error:&error];//调用删除方法 if (error != nil) { MyNSLog(@"删除出错:%@", [error localizedDescription]); }else{ MyNSLog(@"删除成功!"); } return isSucces; } //返回目录下所有的文件名(不包括路径)数组 + (NSArray *)allFilesInFolderPath:(NSString *)folderPath{ return [FileDefaultManager contentsOfDirectoryAtPath:folderPath error:nil]; } //返回目录下所有文件的名称(不包括路径)和大小 + (NSMutableArray *)allFilesAttributeInFolderPath:(NSString *)folderPath{ NSArray *filesName = [FileDefaultManager contentsOfDirectoryAtPath:folderPath error:nil]; NSMutableArray *fileAttributes = [NSMutableArray new]; for (NSString *fileName in filesName) { NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];//完整路径 BOOL isDir;//判断该路径是不是文件夹 BOOL isExists = [FileDefaultManager fileExistsAtPath:filePath isDirectory:&isDir]; if (![fileName.lowercaseString hasSuffix:@"ds_store"] && isExists && !isDir) {//过滤文件夹和索引文件.DS_Store NSError *error = nil; NSDictionary *attributes = [FileDefaultManager attributesOfItemAtPath:filePath error:&error]; NSDictionary *dicTemp = [[NSDictionary alloc] initWithObjectsAndKeys: fileName,@"filename", [NSString stringWithFormat:@"%llu",[attributes fileSize]],@"filesize", nil]; [fileAttributes addObject:dicTemp]; dicTemp = nil; } } return fileAttributes; } //删除目录下所有文件和文件夹 + (void)filesDelete:(NSString *)folderPath{ NSArray *files = [self allFilesInFolderPath:folderPath]; MyNSLog(@"files count=%d", [files count]); for (NSString *file in files) { NSString *filePath = [folderPath stringByAppendingPathComponent:file];//转换成url BOOL isDir;//判断该路径是不是文件夹 BOOL isExists = [FileDefaultManager fileExistsAtPath:filePath isDirectory:&isDir];//返回文件或文件夹是否存在 if (isExists) { [self fileDelete:filePath]; } } } //创建文件夹 + (BOOL)folderCreate:(NSString *)folderPath{ NSError *error = nil; BOOL isSucces=NO; BOOL isDir;//判断该路径是不是文件夹 BOOL isExists = [FileDefaultManager fileExistsAtPath:folderPath isDirectory:&isDir];//返回文件或文件夹是否存在 if (isExists && isDir) { MyNSLog(@"%@文件夹已经存在!不需要创建!",folderPath); return YES; } MyNSLog(@"开始创建文件夹%@",folderPath); isSucces=[FileDefaultManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error]; if (error != nil) { MyNSLog(@"创建出错:%@", [error localizedDescription]); }else{ MyNSLog(@"创建成功!"); } return isSucces; }