IOS文件系统

/*
     ios文件系统
     当第一次启动app时,ios操作系统就为此app创建了一个文件系统,该文件系统下就默认有4个目录,分别是:
     Documents: 存储用户在操作app时产生的数据,次目录下地数据可以通过iCloud进行同步
     
     Library: 用户偏好设置数据,通常和此类NSUserDefaults搭配使用,此目录下地数据可以通过iCloud进行同步
     
     tmp: 存放临时数据(缓存),此目录下地数据不会通过iCloud进行同步
     
     app: 开发者不会操作此目录,通常是通过此类NSBundle类来获取包内资源,如工程素材
     */
    
    //获取程序根目录
    NSString *rootPath = NSHomeDirectory();
    
    //获取程序根目录下的Documents
    NSString *documentsPath = [rootPath stringByAppendingFormat:@"/%@",@"Documents"];
    //或者
    documentsPath = [rootPath stringByAppendingPathComponent:@"Documents"];
    
    NSLog(@"documentsPath = %@",documentsPath);
    
    //常用的获取documents目录方法
    /*
     <#NSSearchPathDirectory directory#> :指定了搜索的路径名称
     <#NSSearchPathDomainMask domainMask#> :限定了文件的检索范围只在沙盒(sandbox)内部
     <#BOOL expandTilde#> :决定了是否展开波浪线符号,展开后才是完整的路径,这个布尔值设为YES时才会产开路径
     
     该方法返回的为一个数组,在iphone中由于只有一个唯一路径,所以直接取数组第一个元素即可
     */
    documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSLog(@"documentsPath = %@",documentsPath);
    
    //下载一个视频文件到Documents目录下地Video文件夹
    
    //这里我们封装一个函数,使得这个函数返回的时Documents目录下想要的文件夹的路径

    NSString *videoPath = [self creatDirInDocuments:@"Video"];
    NSLog(@"videoPath = %@",videoPath);
    
    if (videoPath != nil) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *videoString = @"http://v8.tv.cctv5.cctv.com/r5wbth/4d/e7/4de76971-63f4-4717-f28b-03d757a7704f/mp4h.mp4";
        //截取videoString最后一个/后的字符串
        NSString *fileVideo = [videoPath stringByAppendingPathComponent:[videoString lastPathComponent]];
        //如果文件夹不存在该路径
        if (![fileManager fileExistsAtPath:fileVideo])
        {
            //进行编码
//            [videoString stringByAddingPercentEscapesUsingEncoding:<#(NSStringEncoding)#>];
            NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
            
            videoString = [videoString stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
            //我们的图片,视频,音频在网络中都是以二进制文件传输,所以这里我们拿到的是data
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:videoString]];
            
            if (data == nil) {
                NSLog(@"网络不给力,请稍后在试");
            }
            else
            {
                //用单例类NSFileManager的对象,将文件写入本地
                BOOL isSuccess = [fileManager createFileAtPath:fileVideo contents:data attributes:nil];
                if (isSuccess) {
                    NSLog(@"视频下载成功");
                }
                else
                {
                    NSLog(@"视频下载失败");
                }

            }
        }
    }

    //字符串拼接,获取Library目录
    NSString *libraryPath = [rootPath stringByAppendingPathComponent:@"Library"];
    NSLog(@"libraryPath = %@",libraryPath);
    
    //第二种
    libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"libraryPath = %@",libraryPath);
    
    //tmp
    NSString *tmpString = NSTemporaryDirectory();
    NSLog(@"tmpString = %@",tmpString);
    

    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",@"http://h.hiphotos.baidu.com/image/pic/item/30adcbef76094b366b2389d7a4cc7cd98d109d53.jpg",@"http://d.hiphotos.baidu.com/image/pic/item/b17eca8065380cd72aaf30d7a644ad3459828153.jpg"];
    
    //下载一些图片,放在tmp目录下地Imgs文件夹下,如果有些图片已经下载了,那么不会继续下载
    NSString *imgsTmpPath =[self creatDirInTmp:@"Imgs"];
    if (imgsTmpPath != nil) {
        NSLog(@"imgsTmpPath = %@",imgsTmpPath);
        for (int i=0; i<imgsArray.count; i++) {
            NSString *imgsString = [imgsArray[i] lastPathComponent];
            
            //获得每张图片的路径
            NSString *imgsPath = [imgsTmpPath stringByAppendingPathComponent:imgsString];
            
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if (![fileManager fileExistsAtPath:imgsPath])
            {
                NSString *urlString = [imgsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
                
                if (data == nil) {
                    NSLog(@"网路有问题,请稍后在试");
                }
                else
                {
                    BOOL isSuccess = [data writeToFile:imgsPath atomically:YES];
                    if (isSuccess) {
                        NSLog(@"图片下载成功");
                    }
                    else
                    {
                        NSLog(@"图片下载失败");
                    }
                }
            }
            
        }
    }
    
    //将字符串写入本地
    NSString *targetString = @"Hello guys";
    BOOL flag = [targetString writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"a.txt"] atomically:YES encoding:4 error:nil];
    if (flag) {
        NSLog(@"成功将字符串写入本地");
    }
    else
    {
        NSLog(@"将字符串写入本地失败");
    }
    
    //将数字写入本地文件
    NSArray *array = @[@"都是",@"爱情",@"惹得",@"祸"];
    flag = [array writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"array.txt"] atomically:YES];
    if (flag) {
        NSLog(@"成功将数组写入本地");
    }
    else
    {
        NSLog(@"将数组写入本地失败");
    }
    
    //将字典写入本地
    NSDictionary *dictionary = @{@"name":@"Rick",@"age":@"25",@"address":@"GZ"};
    flag = [dictionary writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"dic.txt"] atomically:YES];
    if (flag) {
        NSLog(@"成功将地址写入本地");
    }
    else
    {
        NSLog(@"将地址写入本地失败");
    }
    
    //计算文件大小
    //获得将要计算的文件夹
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //获得imgs目录下文件名组成的数组
    NSArray *imgsFileArray = [fileManager subpathsAtPath:imgsTmpPath];
    
    NSLog(@"imgsFileArray = %@",imgsFileArray);
    
    CGFloat count = 0;
    
    for (NSString *ele in imgsFileArray) {
        NSData *data = [NSData dataWithContentsOfFile:[imgsTmpPath stringByAppendingPathComponent:ele]];
        count += data.length;
    }
    
    count = count/1024/1024;
    NSLog(@"缓存文件的大小为%.2f M",count);
    
    //删除文件
    for (NSString *ele in imgsFileArray)
    {
        BOOL isSuccess = [fileManager removeItemAtPath:[imgsTmpPath stringByAppendingPathComponent:ele] error:nil];
        if (isSuccess) {
            NSLog(@"删除成功");
        }
        else
        {
            NSLog(@"删除失败");
        }
    }
    
    //app包,获取包内图片,显示在UI
    NSBundle *bundle = [NSBundle mainBundle];
    NSLog(@"bundle = %@",bundle);
    
    NSString *imgPath = [bundle pathForResource:@"023b5bb5c9ea15ce9c017233b1003af33a87b219" ofType:@".jpg"];
    
    NSData *data = [NSData dataWithContentsOfFile:imgPath];
    
    NSLog(@"data = %@",data);
    
    self.imageview.image = [UIImage imageWithData:data];

}


-(NSString *)creatDirInDocuments:(NSString *)dirName
{
    //获得Documents文件路径
    NSString *documentsPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 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;

}



-(NSString *)creatDirInTmp:(NSString *)dirname
{
    NSString *tmp = NSTemporaryDirectory();
    
    //想要文件的路劲
    NSString *dirPath = [tmp 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) {
            dirPath = nil;
            NSLog(@"error = %@",error.debugDescription);
        }
    }
    return dirPath;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值