//拷贝小文件
- (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];
}
iOS中实现文件拷贝
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 每次用到那些权限的相关描述都是上百度,今天索性整理一下。按照常用指数进行罗列 1.常用指数:❤️ ❤️ ❤️ ❤️...
- iOS 逆向 - 如何将设备中的文件通过 scp 拷贝回本地 Mac 新手在使用 scp 的时候,一般都知道如何将...
- 译者注:本文是对 Apple 官方文档的翻译,原文地址为:https://developer.apple.com/...