iOS本地文件操作

简介: 文件操作在我们开发过程中或多或少都会遇到,我一般不会去记这些,每次使用的时候都要去查询下,有点麻烦,今天索性记录下,方便查找!

文件操作在我们开发过程中或多或少都会遇到,我一般不会去记这些,每次使用的时候都要去查询下,有点麻烦,今天索性记录下,方便查找!

  1. 写文件(保存文件)


//保存文件
 - (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(@"哈哈哈哈哈写入文件成功");
 }


  1. 查找文件


//获取文件数据
 - (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];
 }


  1. 删除文件


//删除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(@"文件不存在");
     }
 }



相关文章
|
4月前
|
iOS开发
你知道IOS移动端到操作手势有哪些吗?
你知道IOS移动端到操作手势有哪些吗?
|
10月前
|
Android开发 iOS开发 Windows
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
无影ios & Android客户端6.0.0版本发布!移动端触控体验更舒适,用户操作更便捷,一起来看看!
682 0
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
|
iOS开发
你知道IOS移动端到操作手势有哪些吗?
大家好,我是阿萨。随着移动端设置应用越来越多。大家在移动端设备上的操作手势有哪些呢?今天阿萨给大家梳理下IOS移动端操作的标准手势。快来看下,看完拿自己公司APP练一下手。
189 0
|
Web App开发 iOS开发
iOS开发 - 网页拉起app后并执行某些操作
iOS开发 - 网页拉起app后并执行某些操作
172 0
iOS开发 - 网页拉起app后并执行某些操作
|
JavaScript Android开发 iOS开发
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
267 0
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
|
JavaScript 前端开发 Android开发
根据js来判断手机是操作系安卓还是ios
根据js来判断手机是操作系安卓还是ios
528 0
|
开发框架 关系型数据库 MySQL
使用iOS原生sqlite3框架对sqlite数据库进行操作(五)
使用iOS原生sqlite3框架对sqlite数据库进行操作
169 0
|
SQL 数据库 iOS开发
使用iOS原生sqlite3框架对sqlite数据库进行操作(三)
使用iOS原生sqlite3框架对sqlite数据库进行操作
145 0
|
设计模式 数据库 iOS开发
使用iOS原生sqlite3框架对sqlite数据库进行操作(二)
使用iOS原生sqlite3框架对sqlite数据库进行操作
200 0
使用iOS原生sqlite3框架对sqlite数据库进行操作(二)