文件操作在我們開發過程中或多或少都會遇到,我一般不會去記這些,每次使用的時候都要去查詢下,有點麻煩,今天索性記錄下,方便查找!
-
寫文件(保存文件)
//保存文件 - (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(@"文件不存在"); } }