文件操作在我们开发过程中或多或少都会遇到,我一般不会去记这些,每次使用的时候都要去查询下,有点麻烦,今天索性记录下,方便查找!
- 写文件(保存文件)
//保存文件 - (void)writeFile { NSString *lastDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"cadLocalDir"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:lastDir]) { NSLog(@"存在"); } else { NSLog(@"文件夹不存在"); //注意:如果在某个文件夹下写文件,如果文件夹不存在,无法写成功 BOOL isSuccess = [fileManager createDirectoryAtPath:lastDir withIntermediateDirectories:YES attributes:nil error:nil]; if (isSuccess) { NSLog(@"success"); } else { NSLog(@"fail"); } } NSString *aPath = [lastDir stringByAppendingPathComponent:@"d.txt"]; NSString *bPath = [lastDir stringByAppendingPathComponent:@"e.txt"]; NSString *cPath = [lastDir stringByAppendingPathComponent:@"f.txt"]; // 写入文件 注意:写文件时,如果文件名不存在,会自动创建 NSString *a = @"哈哈哈哈哈写入文件"; BOOL isA = [a writeToFile:aPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (isA) NSLog(@"哈哈哈哈哈写入文件成功"); NSString *b = @"哈哈哈哈哈写入文件"; BOOL isB = [b writeToFile:bPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (isB) NSLog(@"哈哈哈哈哈写入文件成功"); NSString *c = @"哈哈哈哈哈写入文件"; BOOL isC = [c writeToFile:cPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (isC) NSLog(@"哈哈哈哈哈写入文件成功"); }
- 查找文件
//获取文件数据 - (void)getFileData { NSString *lastDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"cadLocalDir"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:lastDir]) { //cadLocalDir文件不存在 return; } //lastDir 为文件夹的路径 NSDirectoryEnumerator *myDirectoryEnumerator = [fileManager enumeratorAtPath:lastDir]; //用来存文件信息的数组 NSMutableArray *fileInfosArr = [[NSMutableArray alloc] init]; NSString *file; //遍历当前目录 while ((file = [myDirectoryEnumerator nextObject])) { if ([[file pathExtension] isEqualToString:@"dwg"]) { //取得文件扩展名为.dwg的文件名 NSString *filePath = [lastDir stringByAppendingPathComponent:file]; MlgEFileInfo *fileInfo = [[MlgEFileInfo alloc] init]; fileInfo.fileName = file; //文件名 fileInfo.filePath = filePath; //文件路径 NSError *error = nil; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:&error]; if (fileAttributes) { id creationDate, fileModDate; //文件创建日期 if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) { if ([creationDate isKindOfClass:[NSDate class]]) { fileInfo.fileCreationDate = creationDate; } } //文件修改日期 if ((fileModDate = [fileAttributes objectForKey:NSFileModificationDate])) { if ([fileModDate isKindOfClass:[NSDate class]]) { fileInfo.fileModificationDate = fileModDate; } } } [fileInfosArr addObject:fileInfo]; } } NSArray *sortedFileInfos = [fileInfosArr sortedArrayUsingComparator:^NSComparisonResult(MlgEFileInfo *obj1, MlgEFileInfo *obj2) { return [obj2.fileModificationDate compare:obj1.fileModificationDate]; //降序 }]; [self.dataSource removeAllObjects]; [self.dataSource addObjectsFromArray:sortedFileInfos]; }
- 删除文件
//删除CAD文件 - (void)deleteCadFiel:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; // 判断要删除的文件是否存在 if ([fileManager fileExistsAtPath:filePath]) { NSLog(@"文件存在"); // 删除 BOOL isSuccess = [fileManager removeItemAtPath:filePath error:nil]; NSLog(@"%@",isSuccess ? @"删除成功" : @"删除失败"); } else { NSLog(@"文件不存在"); } }