OC学习:文件读写

iOS文件系统
当开发者第一次启动app时,iOS操作系统就为此APP创建了一个文件系统,该文件系统下默认有四个,分别是:
Documents:存储用户在操作app时产生的数据,此目录下的数据可以通过iCloud进行同步
Library:用户偏好设置数据,通常和此类NSUserDefaults搭配使用,此目录下的数据可以通过iCloud进行同步
tmp:存放临时数据,此目录下的数据不会通过iCloud进行同步

app包:开发者不会操作此目录,通常是通过此类NSBundle类开获取包内资源,如工程素材

//拿到tmp路径
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);
    //获取NSFileManager单例类,用于文件操作
    NSFileManager *fileManager = [NSFileManager defaultManager];
//    NSString *Imgs = [tmpPath stringByAppendingPathComponent:Imgs];
//    NSString *Imgs = [tmpPath stringByAppendingPathComponent:@"Docu"];
    //用NSFileManager单例类判断是否为文件夹是否存在
//    BOOL isExist = [fileManager fileExistsAtPath:Imgs];
//    NSLog(@"%d",isExist);
    //找到程序应用的跟路径
    NSString *rootPath = NSHomeDirectory();
    NSLog(@"%@",rootPath);
    //在跟路径后面添加/Documents就是Documents路径
    NSString *DicumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    //函数获取Document路径。
    DicumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 拿到library路径
    NSString *library = [rootPath stringByAppendingString:@"/library"];
    //错误路径
    library = [rootPath stringByAppendingString:@"library"];
//    NSLog(@"%@",library);
    NSLog(@"%d",[fileManager fileExistsAtPath:library]);
    //函数获取library路径
    library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@",library);
    NSError *error;
    NSString *myPath = [library stringByAppendingString:@"/lin"];
    BOOL isSuccess = [fileManager createDirectoryAtPath:myPath withIntermediateDirectories:YES attributes:nil error:&error];
    NSLog(@"%d",isSuccess);

    NSArray *imageArray = @[@"http://s0.hao123img.com/res/r/image/2015-08-11/cc1eb723b3afec8d1f65d1edbe794349.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/867fdb3ab71b3a31b2c2a763ca84d8ea.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/2bf0ad9342cd778452b6e432fc36626d.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/608a6bde897e66560752247706452dce.jpg",@"nil"];
    int i = 0;
    while (i < imageArray.count) {
        NSString *imageString = [imageArray objectAtIndex:i];
        NSString *imagePath = [myPath stringByAppendingString:[imageString lastPathComponent]];
        if ([fileManager fileExistsAtPath:imagePath]) {
            NSLog(@"已经下载!");
        }else {
            NSString *urlString = [imageString stringByAddingPercentEscapesUsingEncoding:4];
            NSURL *url = [NSURL URLWithString:urlString];
            NSData *imageData = [NSData dataWithContentsOfURL:url];

            if (imageData !=nil) {
                if ([fileManager createFileAtPath:imagePath contents:imageData attributes:nil]) {
                    NSLog(@"下载成功");
                }else {
                    NSLog(@"shibai");
                }

            }
           NSLog(@"%@",NSHomeDirectory());
        }
        i++;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值