NSFileManager
我们来解决一下几个问题:
1、确定文件是否存在
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"file.txt"]; BOOL fileExists = [fileManager fileExistsAtPath:filePath];
2.列出文件里的所有目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *bundleURL = [[NSBundle mainBundle] bundleURL]; NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL includingPropertiesForKeys:@[] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension == 'png'"]; for (NSURL *fileURL in [contents filteredArrayUsingPredicate:predicate]) { // 在目录中枚举 .png 文件 }
3.在目录中递归遍历文件
NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *url = [[NSBundle mainBundle] bundleURL]; NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:url includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) { if (error) { NSLog(@"[Error]%@(%@)", error, url); return NO; } return YES; }]; NSMutableArray *mutableFileURLs = [NSMutableArray array]; for (NSURL *fileURL in enumerator) { //文件名 NSString *fileName; [fileURL getResourceValue:&fileName forKey:NSURLNameKey error:nil]; NSNumber *isDirectory; //是否是文件夹 [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil]; if ([fileName hasPrefix:@"_"] && [isDirectory boolValue]) { [enumerator skipDescendants]; continue; } if (![isDirectory boolValue]) { [mutableFileURLs addObject:fileURL]; } } NSLog(@"%@", mutableFileURLs);
4.创建一个目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *imagesPath = [documentsPath stringByAppendingPathComponent:@"images"]; if (![fileManager fileExistsAtPath:imagesPath]) { [fileManager createDirectoryAtPath:imagesPath withIntermediateDirectories:NO attributes:nil error:nil]; }]
5.删除一个目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"images"]; [fileManager removeItemAtPath:imagePath error:nil];
文件属性的建:
NSFileAppendOnly
: 文件是否只读NSFileBusy
: 文件是否繁忙NSFileCreationDate
: 文件创建日期NSFileOwnerAccountName
: 文件所有者的名字NSFileGroupOwnerAccountName
: 文件所有组的名字NSFileDeviceIdentifier
: 文件所在驱动器的标示符NSFileExtensionHidden
: 文件后缀是否隐藏NSFileGroupOwnerAccountID
: 文件所有组的group IDNSFileHFSCreatorCode
: 文件的HFS创建者的代码NSFileHFSTypeCode
: 文件的HFS类型代码NSFileImmutable
: 文件是否可以改变NSFileModificationDate
: 文件修改日期NSFileOwnerAccountID
: 文件所有者的IDNSFilePosixPermissions
: 文件的Posix权限NSFileReferenceCount
: 文件的链接数量NSFileSize
: 文件的字节NSFileSystemFileNumber
: 文件在文件系统的文件数量NSFileType
: 文件类型NSDirectoryEnumerationSkipsSubdirectoryDescendants
: 浅层的枚举,不会枚举子目录NSDirectoryEnumerationSkipsPackageDescendants
: 不会扫描pakages的内容NSDirectoryEnumerationSkipsHiddenFile
: 不会扫描隐藏文件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 电商平台中订单未支付过期如何实现自动关单?
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 如何开发 MCP 服务?保姆级教程!
· 1.net core 工作流WorkFlow流程(介绍)
· 瞧瞧别人家的限流,那叫一个优雅!
· 从零散笔记到结构化知识库:我的文档网站建设之路
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱