##一、文件保存的目录结构 iOS的文件存储采用的是沙盒目录机制,没有SD卡保存的概念。一个APP安装成功后就会对应一个文件目录这个目录就叫沙盒。这个APP的全部文件都会保存在这个目录中,每一个APP沙盒独立互相不能访问目录,沙盒目录中包含Documents、Library、Tmp三个子目录。结构以下:缓存
// NSHomeDirectory() 获取沙盒目录 NSString *homePath = NSHomeDirectory(); NSLog(@"%@",homePath);
// command + shift + G 能够前往该文件夹 /Users/qinglinfu/Library/Developer/CoreSimulator/Devices/039371EC-EC36-40B3-9CB8-3065925EDB7D/data/Containers/Data/Application/55F7965F-0D92-4E88-B422-E423C9A732E1
####Documents目录: 用于存储用户数据,iTunes备份和恢复的时候会包括此目录,程序建立产生的或在程序中浏览保存的文件数据放在该目录下,本地持久化的数据放在这个目录中。dom
directory
:要查找的目录atom
domainMask
:从哪一个主目录中获取(一般是沙盒目录)code
expandTilde
: 是否返回完整的路径对象
// 获取目录路径的方法 NSArray<NSString *> *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);
// NSDocumentDirectory: documents目录 // NSUserDomainMask: 当前沙盒目录 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = NSTemporaryDirectory();
##二、NSFileManager管理文件目录图片
iOS中的文件管理由NSFileManager类实现,负责文件\目录的增删改查、获取数据属性等。rem
// NSFileManager是一个单例对象,只实例化一次。 NSFileManager *manager = [NSFileManager defaultManager];
// 获取documents目录,将建立的新目录保存在该目录下 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 目录的保存路径 NSString *directoryPath = [docPath stringByAppendingPathComponent:@"datas"]; // 使用NSFileManager 建立目录 BOOL isSuccess = [manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil]; if (isSuccess) { NSLog(@"建立成功!"); } else { NSLog(@"建立失败!"); }
// 建立文件的保存路径 NSString *filePath = [directoryPath stringByAppendingPathComponent:@"userInfo.plist"]; //使用 NSFileManager 建立文件 BOOL isSuccess = [manager createFileAtPath:filePath contents:nil attributes:nil]; if (isSuccess) { NSLog(@"建立成功!"); } else { NSLog(@"建立失败!"); }
// 使用NSFileManager 移除指定路径的文件 BOOL isSuccess = [manager removeItemAtPath:directoryPath error:nil]; if (isSuccess) { NSLog(@"删除文件成功!"); } else { NSLog(@"删除文件失败!"); }
// 将字典持久保存到建立的plist文件中 NSMutableDictionary *info = [NSMutableDictionary dictionary]; [info setObject:@"2015年10月26日13:05:46" forKey:@"date"]; [info setObject:@"这个是持久化保存的数据内容" forKey:@"content"]; // 写入数据 BOOL isSuccess = [info writeToFile:filePath atomically:YES]; if (isSuccess) { NSLog(@"数据写入成功!"); } else { NSLog(@"数据写入失败!"); }
// 从保存的路径中读取对应数据类型的数据 NSDictionary *userInfo = [NSDictionary dictionaryWithContentsOfFile:filePath]; if (userInfo) { NSLog(@"%@",userInfo); } else { NSLog(@"数据读取失败!"); }
// 获取指定单个文件的属性 //NSDictionary *infoDic = [manager attributesOfItemAtPath:filePath error:nil]; // 获取指定目录下全部子文件路径 NSArray *subPaths = [manager subpathsAtPath:directoryPath]; // 获取每一个子文件的属性 for (NSString *subPath in subPaths) { NSString *subPathString = [directoryPath stringByAppendingPathComponent:subPath]; NSDictionary *fileAttribute = [manager attributesOfItemAtPath:subPathString error:nil]; NSLog(@"%@",fileAttribute); }
// 文件属性列表 NSFileCreationDate = "2015-10-28 02:48:44 +0000"; NSFileExtensionHidden = 0; NSFileGroupOwnerAccountID = 80; NSFileGroupOwnerAccountName = admin; NSFileModificationDate = "2015-10-28 02:48:44 +0000"; NSFileOwnerAccountID = 501; NSFilePosixPermissions = 420; NSFileReferenceCount = 1; NSFileSize = 327; NSFileSystemFileNumber = 14750886; NSFileSystemNumber = 16777224; NSFileType = NSFileTypeRegular;