OC_iOS的文件系统(Document、Library、tmp、app包)

时间:2022-11-01 09:13:11

iOS文件系统
当第一次启动app 的时候,iOS操作系统就为此app创建一个文件系统,该文件系统下,默认有四个目录

/*

Document:存储用户在操作app 时产生的数据,在此目录下的数据可以通过iCloud进行同步
Library:用户偏好设置,通常和此类NSUserDefaults 搭配使用,在此目录下的数据可以通过iCloud进行同步
tmp:存放临时数据,此目录下的数据不会通过iCloud进行同步
app包:开发者不会操作此目录,通常是通过此类NSBundle来获取包内资源,例如工程的素材

*/

获取app包的路径的方法

[NSBundle mainBundle];

获取程序的根目录

NSString *rootPath = NSHomeDiretory();

获取根目录下的Documents

NSString *Documents = [rootPath stringByAppendingPathComponent:@"Documents"];

NSString *Documents1 = [rootPath stringByAppendingFormat:@"/Documents"];

最常用的获取Documents目录的方式

NSArray *array = [ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//第一个参数为查找的是Documents目录
//第二个参数指定的是将搜索限制在应用程序沙盒中
//第三个参数如果为YES会将检索后的路径中~符号展开为实际路径


NSString *Document2 = [array objectAtIndex:0];

Documents1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDiretory,NSUserDomainMask,YES) objectAtIndex:0];

//下载一个视频文件到Documents目录下的Video文件

NSString *videoPath = [self creatDicInDoments:"Video"]
//-(NSString *)creatDicInDocuments:(NSString *)dirName 创建的方法

if(videoPath != nil){
NSString *videoUrlString = @"xxx.mp4";

//lastPathComponent 获得带后缀的文件名
//pathExtension 不带'.'
NSString *ddd = [videoUrlString lastPathComponent ];

//NSString *filePath = [videoPath stringByAppendingPathComponent];

//如果文件夹中不存在该路径
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]){
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrlString]];

//用NSFileManager的对象,将文件写入本地
BOOL isSuccess = [fileManager createFileAtPath:filePath contents:data attributes:nil];

if(isSuccess){
NSLog(@"视频下载成功");
NSLog(@"%@",filePath);
}
else{
NSLog(@"下载失败");
}
}
}

获取Library目录

NSString *LibraryPath = [rootPath stringByAppendingPathComponent:@"Library"];
LibraryPath = [NSSearchPathForDiretoriesInDomains(NSLibraryDiretory,NSUserDomainMask,YES) objectAtIndex:0];

//tmp
NSString *tmpString = NSTemporaryDirectory();

NSString *imgsDocumentsPath = [self createDirInTmp:@”Imgs”];

NSArray *imgsArray = @[@”aa.jpg,xx.jpg, bb.jpg”];

if ( imgsDocumentsPath != nil ){
for ( int i =0 ;i < imgsArray.count ;i++ ){
NSString *imgsString = [imgsArray[i] lastPathComponent];
//每一张图片的路径
NSString *imgsPath = [imgsDocumentsPath stringByAppendingPathComponent:imgsString];
NSFileManager *fileManager =[NSFileManager dafaultManager];
if( ![fileManager fileExistsAtPath:imgsPath] ){
//将每张图片的url进行编码(有一些有中文)
[NSCharacterSet URLQueryAllowedCharacterSet]
NSString *urlString = [imgsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSData *data = [NSData dataWithContentsOfURL:[NSRUL URLWithString:urlString]];

if (data == nil){
NSLog(@"网络炸了,一会儿再搞");
}
else{
BOOL isSuccess = [data writeToFile:imgsPath atomically:YES];
if(isSuccess){
NSLog(@"图片下载成功");
}
else{
NSLog(@"下载失败");
}
}
}
else{
NSLog(@"图片已存在");
}
}
}

将字符串写入本地
NSStrinG *targetString = @”sbnimasi l “;
NSError *error;
BOOL flag = [targetString writeToFile:[imgsDocumentsPath stringByAppendingPathComponent:@”a.txt”] atomically:YES encoding:4 error:&error];
if(flag){
NSLog(@”字符串写入成功”);
}
else{
NSLog(@”失败了 操”);
}

//将数组写入本地
NSArray *array1 = @[@”马蓉”,@”宋祖蓝”,@”宝宝”,@”sb”];
flag = [array1 writeToFile:[imgsDocumentPath stringByAppendingPathComponent:@”array1.txt”] atomically:YES];
if(flag){
NSLog(@”数组写入成功”);
}
else{
NSLog(@”失败”);
}

//将字典写入本地
NSDictionary *dic = @{@”sb”:@”1”,@”sb2”:@”2”,@”sb3”:@”3”};
flag = [dic writeToFile:[imgsDocumentsPath stringByAppendingPathComponent:@”dic.txt”] atomically:YES];
if (flag) {
NSLog(@”写入成功”);
}

//计算文件夹大小
NSFileManager *fileManager = [NSFileManager defaultManager];
//所有文件下的所有文件名
NSArray *fileManagerArray = [fileManager subpathsAtPath:imgsDocumentsPath];

CGFloat count = 0.0;
for (NSString *ele in fileManagerArray){
NSData *data = [NSData dataWithContentsOfFile:[imgsDocumentsPath stringByAppendPathComponent:ele]];
count += data.length;
}
count = count / 1024 / 1024
//%.2f 保留2位小数
NSLog(@”缓存文件的大小为%.2fMB”,count);

//删除文件

for (NSString *ele in fileManagerArray){

BOOL *flag = [fileManager removeItemAtPath:[imgsDocumentsPath stringByAppendingPathComponent:ele] error:nil];
if(flag){
NSLog(@"删除成功");
}
}

app包,获取包内的图片,显示在UI上

NSBundle *bundle = [NSBundle mainBundle];
NSString *imgPath = [bundle pathForResource:@”8EDD2DE79FF1553E50C64DCCB261BDE8” ofType:@”.jpg”];
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];

/*
通过UI控制器拉到@interface就会生成
@property (weak, nonatomic) IBOutlet UIImageView picImageView;
*/
self.picImageView.image = [UIImage imageWithData:imgData];

}//-(void)viewDidLoad

-(NSString *)createDicInDocuments:(NSString *)dirName{
//获取Documents的文件路径
NSString *documents2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDictory,NSUserDomainMask,YES) objectAtIndex:0];

//拼接成我们想要的文件的路径的字符串
NSString *dirDocuments = [documents2 stringByAppendinGPathComponent"dirName];
}

//NSFileManager 单例类(只有一个对象),用于文件的操作
//单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。

NSFileManager *fileManager = [NSFileManager defaultManager];

//判断本地是否存在某个文件或文件夹

BOOL isExist = [fileManager fileExistsAtPath:dirDocuments];
if(!isExist){
//创建文件夹
NSError *error;
BOOL isSuccess = [fileManager createDirectoryAtPath:dirDocuments withIntermediateDirectories:YES attributes:nil error:&error];

if(!isSuccess){
//如果文件创建失败
NSLog(@"error = %@",error.debugDescription);
dirDocuments = nil;
}
}
return dirDocuments;
}

-(NSString *)createDirInTmp:(NSString *)dirName{

NSString *tmpPath = NSTemporaryDirectory();
//想要文件夹路径
NSString *dirPath = [tmpPath stringByAppendingPathComponent:dirName];

NSFileManager *fileManager = [NSFileManager defaultManager];//创建单例判断
if(![fileManager fileExistsAtPath:dirPath]){
NSError *error;
BOOL isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
if (!isSuccess){
NSLog(@"error = %@",error.description);
dirPath = nil;
}
}
return dirPath;
}