OC-文件系统操作
来源:互联网 发布:mac os sierra文件 编辑:程序博客网 时间:2024/06/07 15:55
ios的文件系统(沙盒机制)
当第一次启动app 时,ios操作系统就为此app 创建一个文件系统,该文件系统下,默认有四个目录,分别是:
Documents:存储用户在操作app 时产生的数据,在此目录下的数据可以通过iCloud进行同步Library:用户偏好设置,通常和 NSUserDefaults 搭配使用,在此目录下的数据可以通过iCloud进行同步tmp:存放临时数据,在此目录下的数据不会通过iCloud进行同步app包:开发则不会操作此目录,通常是通过 NSBundle 来获取包内资料,如工程的素材
沙盒机制:
1.每个应用程序都在自己的沙盒内
2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容
3.应用程序向外请求或接收数据都需要经过权限认证
对文件系统的一些相关操作:
//获取程序根目录NSString *rootPath = NSHomeDirectory();
//获取根目录下 Documents 的目录//在根目录后面拼接 Documents,以此来获得 Documents 的目录NSString *documentsPath = [rootPath stringByAppendingPathComponent:@"Documents"];//最常用的获取 Documents 的目录方式documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)objectAtIndex:0 ];
如何下载一个视频文件到 Documents 目录下的 Video 文件夹
//首先我们创建一个方法,用来创建 Video 文件夹- (NSString *)createDirInDocuments:(NSString *)dirName{ //获取 Documents 的文件路径 NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) objectAtIndex:0]; //拼接成我们想要的文件的路径的字符串 NSString *dirDocuments = [documentsPath 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;}
//先调用方法,创建一个Video的文件夹NSString *videoPath = [self createDirInDocuments:@"Video"]; //判断文件夹是否存在,如果存在,执行以下代码 if (videoPath != nil) { NSString *videoUrlString = @"http://163.177.2.46/youku/6775926E5DA33823CA59652EE5/0300080100536FBEDAABC00172D4733C053123-91E7-7011-9F9B-ECC617DC9593.mp4"; NSString *filePath = [videoPath stringByAppendingPathComponent:[videoUrlString lastPathComponent]]; //如果文件夹中不存在该路径 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(@"视频下载成功"); } else{ NSLog(@"视频下载失败"); } } }
如何下载图片到 Tmp 里面的 Imgs 文件夹
//首先我们创建一个方法,用来创建 Imgs 文件夹//与上面创建 Video 文件夹类似,注释不重复- (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.debugDescription); dirPath = nil; } } return dirPath;}
//调用方法,创建一个 Imgs 文件夹NSString *imgsDocumentsPath = [self createDirInTmp:@"Imgs"];//定义一个数组,存放三张图片的网络地址NSArray *imgsArray = @[@"http://d.hiphotos.baidu.com/image/pic/item/6a63f6246b600c331c964cf21d4c510fd9f9a119.jpg",@"http://c.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce9c017233b1003af33a87b219.jpg",@"http://f.hiphotos.baidu.com/image/pic/item/e1fe9925bc315c60d916f9d58ab1cb134954770d.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 defaultManager]; if (![fileManager fileExistsAtPath:imgsPath]) { //将每张图片的url进行编码 NSString *urlString = [imgsArray[i]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //拿到网络图片的data NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; if (data == nil) { NSLog(@"网络有问题,请稍后再下载"); } else{ BOOL isSuccess = [data writeToFile:imgsPath atomically:YES]; if (isSuccess) { NSLog(@"图片下载成功"); } else{ NSLog(@"图片下载失败"); } } } else{ NSLog(@"图片已存在"); } } }
下面将介绍如何将字符串、字典、数组写入本地
//将字符串写入本地,使用 writeToFile: 关键字NSString *targetString = @"看这条博客的都是好人,没事就点个赞";BOOL flag = [targetString writeToFile:[imgsDocumentsPath stringByAppendingPathComponent:@"a.txt"] atomically:YES encoding:4 error:nil];if (flag) { NSLog(@"写入成功");}else{ NSLog(@"写入失败");}
//将数组写入本地NSArray *array1 = @[@"博人",@"帅哥",@"美女"];flag = [array1 writeToFile:[imgsDocumentsPath stringByAppendingPathComponent:@"array1.txt"] atomically:YES];if (flag) { NSLog(@"写入成功");}else{ NSLog(@"写入失败");}
//将字典写入本地NSDictionary *dictionary = @{@"name":@"moon",@"age":@20};flag = [dictionary writeToFile:[imgsDocumentsPath stringByAppendingPathComponent:@"dictionary.txt"] atomically:YES];if (flag) { NSLog(@"写入成功");}else{ NSLog(@"写入失败");}
在OC中如何计算文件大小
//初始化一个单例NSFileManager *fileManager = [NSFileManager defaultManager];//将文件夹里所有文件的名称都放在数组里NSArray *fileManagerArray = [fileManager subpathsAtPath:imgsDocumentsPath];//创建一个count 用来接收累加每个文件的大小CGFloat count = 0.0;//遍历数组for (NSString *ele in fileManagerArray) { NSData *data = [NSData dataWithContentsOfFile:[imgsDocumentsPath stringByAppendingPathComponent:ele]]; count += data.length;}count = count/1024/1024;//将b单位转换为MNSLog(@"缓存文件的大小为:%.2fM",count);
如何删除文件,清除缓存
//遍历数组for (NSString *ele in fileManagerArray) { BOOL isSuccess = [fileManager removeItemAtPath:[imgsDocumentsPath stringByAppendingPathComponent:ele] error:nil]; if (isSuccess) { NSLog(@"删除成功"); } else{ NSLog(@"删除失败"); }}
0 0
- OC-文件系统操作
- 操作文件系统
- 文件系统操作
- 文件系统操作
- 文件系统操作
- 文件系统操作
- OC -- NSFileManager文件系统的管理
- OC学习日记016-IOS文件系统
- oc 字符串操作
- OC文件操作
- OC -- NSString 操作
- oc 字符串操作
- OC文件操作
- OC 字符串操作
- oc 字符串操作
- oc 字符串操作
- OC的文件操作
- OC字符串操作
- Android中的Environment.getExternalStorageState使用
- ajax请求中文参数在action中乱码问题
- iOS支持IPV6
- mac下 ruby version 升级2.2.2
- Codeforces Problem 710B Optimal Point on a Line(距离之和最小)
- OC-文件系统操作
- 在水经注微图中如何复制已绘制对象
- 关于夺宝类项目,审核问题
- retrofit2上传文件总结
- jquery.cookie中的操作
- 算法之----欧拉回路,欧拉通路,半欧拉图
- bootbox 使用方式
- 关于UIView的autoresizingMask属性的研究
- Linux使用tcpdump命令抓包保存pcap文件wireshark分析