iOS文件拷贝实现

51 篇文章 0 订阅
//拷贝小文件
- (IBAction)copyNormalFile:(id)sender {
    //需求:/Documents/source.txt -> 拷贝到/Docuements/target.txt

    //1.两个文件所在的路径
    NSString *sourcePath = [self.documentsPath stringByAppendingPathComponent:@"source.txt"];
    NSString *targetPath = [self.documentsPath stringByAppendingPathComponent:@"target.txt"];
    //2.创建两个空的文件
    NSString *sourceContent = @"源文件的测试内容。。。";
    [[NSFileManager defaultManager] createFileAtPath:sourcePath contents:[sourceContent dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [[NSFileManager defaultManager] createFileAtPath:targetPath contents:nil attributes:nil];
    //3.使用NSFileHandle写入source.txt内容
    //3.1 创建NSFileHandle对象,指定哪个文件,并且指定目的(读或者写)
    NSFileHandle *sourceHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
    NSFileHandle *targetHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];
    //4.读取source.txt所有数据
    NSData *readData = [sourceHandle readDataToEndOfFile];
    //5.在写入target.txt
    [targetHandle writeData:readData];
    //测试:在给定要写入的数据
    NSString *writeContent = @"新写入的数据";
    NSData *writeDataAgain = [writeContent dataUsingEncoding:NSUTF8StringEncoding];
    [targetHandle writeData:writeDataAgain];
}
//拷贝大文件
- (IBAction)copyBigFile:(id)sender {
    //准备:把大的文件的放在/Documents/source.pdf
    //需求:大的source.pdf的内容分批地拷贝到target.pdf
    //1.获取两个文件的路径
    NSString *sourcePath = [self.documentsPath stringByAppendingPathComponent:@"source.pdf"];
    NSString *targetPath = [self.documentsPath stringByAppendingPathComponent:@"target2.pdf"];
    //2.创建空的target.pdf文件
    [[NSFileManager defaultManager] createFileAtPath:targetPath contents:nil attributes:nil];
    //3.创建两个NSFileHandle对象
    NSFileHandle *sourceHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
    NSFileHandle *targetHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];
    //4.while循环分批拷贝
    //设定每次从源文件读取5000bytes
    int dataSizePerTimes = 5000;
//    //源文件的总大小(方式一)
//    NSDictionary *sourceFileDic = [[NSFileManager defaultManager] attributesOfItemAtPath:sourcePath error:nil];
//    NSLog(@"源文件pdf的属性字典:%@", sourceFileDic);
//    //单位:bytes
//    NSNumber *fileSize = [sourceFileDic objectForKey:NSFileSize];
//    int fileTotalSize = [fileSize intValue];
    /*源文件的总大小(方式二)
      坑:如下的方法会把源文件handle对象直接指向最后
     */
    unsigned long long fileTotalSize = [sourceHandle seekToEndOfFile];
    //把挪动到最后的文件指针挪到最前面(相对于文件的开头的偏移量offset)
    [sourceHandle seekToFileOffset:0];
    //已经读取源文件的总大小
    int readFileSize = 0;

    //while循环
    while (1) {
        //计算剩余没有读取的数据的大小
        int leftSize = fileTotalSize - readFileSize;
        //情况一:剩余不足5000bytes
        if (leftSize < dataSizePerTimes) {
            //直接读取剩下的所有数据
            NSData *leftData = [sourceHandle readDataToEndOfFile];
            //写入目标文件
            [targetHandle writeData:leftData];
            //跳出循环
            break;
        } else {
            //情况二:每次读取5000bytes
            NSData *data = [sourceHandle readDataOfLength:dataSizePerTimes];
            //写入目标文件
            [targetHandle writeData:data];
            //更新已经读取的数据大小
            readFileSize += dataSizePerTimes;
        }
    }

    //收尾工作(关闭指向)
    [sourceHandle closeFile];
    [targetHandle closeFile];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值