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单位转换为M
NSLog(@"缓存文件的大小为:%.2fM",count);
如何删除文件,清除缓存
//遍历数组
for (NSString *ele in fileManagerArray) {
BOOL isSuccess = [fileManager removeItemAtPath:[imgsDocumentsPath stringByAppendingPathComponent:ele]
error:nil];
if (isSuccess) {
NSLog(@"删除成功");
}
else{
NSLog(@"删除失败");
}
}