• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS 使用OC实现文件拷贝

武飞扬头像
绿毛水怪!
帮助1

由于项目需要,需要将文件及内容拷贝一份,考虑到后期维护及复用,单独封装了一个函数.

- (BOOL)copyFile:(NSString *)srcFilePath
     dstFilePath:(NSString *)dstFilePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:dstFilePath]) {
        //创建空的endFilePath文件
        [[NSFileManager defaultManager] createFileAtPath:dstFilePath contents:nil attributes:nil];
    }
    //3.创建两个NSFileHandle对象
    NSFileHandle *sourceHandle = [NSFileHandle fileHandleForReadingAtPath:srcFilePath];
    NSFileHandle *targetHandle = [NSFileHandle fileHandleForWritingAtPath:dstFilePath];
    //4.while循环分批拷贝
    //设定每次从源文件读取5000bytes
    int dataSizePerTimes = 5000;
    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];
    return true;
}
学新通

项目中考虑到文件的大小,索性就采用大文件的处理方式,如果在实际开发中,文件不是很大的话,也可以采用下面的方式

//拷贝小文件
- (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];
}
学新通

可以根据自己需求选择!!!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgicfjk
系列文章
更多 icon
同类精品
更多 icon
继续加载