iOS本地文件操作

文件操作在我們開(kāi)發(fā)過(guò)程中或多或少都會(huì)遇到,我一般不會(huì)去記這些,每次使用的時(shí)候都要去查詢下,有點(diǎn)麻煩,今天索性記錄下,方便查找!

  1. 寫(xiě)文件(保存文件)

     //保存文件
     - (void)writeFile {
         NSString *lastDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"cadLocalDir"];
         NSFileManager *fileManager = [NSFileManager defaultManager];
         if ([fileManager fileExistsAtPath:lastDir]) {
             NSLog(@"存在");
         } else {
             NSLog(@"文件夾不存在");
             //注意:如果在某個(gè)文件夾下寫(xiě)文件,如果文件夾不存在,無(wú)法寫(xiě)成功
             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"];
        
         // 寫(xiě)入文件 注意:寫(xiě)文件時(shí),如果文件名不存在,會(huì)自動(dòng)創(chuàng)建
         NSString *a = @"哈哈哈哈哈寫(xiě)入文件";
         BOOL isA = [a writeToFile:aPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
         if (isA) NSLog(@"哈哈哈哈哈寫(xiě)入文件成功");
         NSString *b = @"哈哈哈哈哈寫(xiě)入文件";
         BOOL isB = [b writeToFile:bPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
         if (isB) NSLog(@"哈哈哈哈哈寫(xiě)入文件成功");
         NSString *c = @"哈哈哈哈哈寫(xiě)入文件";
         BOOL isC = [c writeToFile:cPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
         if (isC) NSLog(@"哈哈哈哈哈寫(xiě)入文件成功");
     }
    
  2. 查找文件

     //獲取文件數(shù)據(jù)
     - (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];
         //用來(lái)存文件信息的數(shù)組
         NSMutableArray *fileInfosArr = [[NSMutableArray alloc] init];
         NSString *file;
         //遍歷當(dāng)前目錄
         while ((file = [myDirectoryEnumerator nextObject])) {
             if ([[file pathExtension] isEqualToString:@"dwg"]) {
                 //取得文件擴(kuò)展名為.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;
                     //文件創(chuàng)建日期
                     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];
     }
    
  3. 刪除文件

        //刪除CAD文件
        - (void)deleteCadFiel:(NSString *)filePath {
               NSFileManager *fileManager = [NSFileManager defaultManager];
           // 判斷要?jiǎng)h除的文件是否存在
           if ([fileManager fileExistsAtPath:filePath]) {
             NSLog(@"文件存在");
             // 刪除
             BOOL isSuccess = [fileManager removeItemAtPath:filePath error:nil];
             NSLog(@"%@",isSuccess ? @"刪除成功" : @"刪除失敗");
         } else {
             NSLog(@"文件不存在");
         }
     }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容