在复制大文件时吧文件分成小部分,这样节约内存.
NSString *homePath = NSHomeDirectory() //当前用户主目录
//源文件路径
NSString *srcPath = [homePath stringByAppendingPathComponent:@"我是文件名.xxx"]
//目标文件路径
NSString *targetPath = [homePath stringByAppendingPathComponent:@"我是文件名.xxx"]
//使用NSFileHandle只能读写已存在的文件,不能创建新文件~~~使用NSFileManager 创建文件
NSFileManager *fileManager = [NSFileManager defaultManager]
//创建目标文件
BOOL success = [fileManager createFileAtPath:targetPath contents:nil attributes:nil];
if (success)
NSLog (@"成功");
}
//创建用于读取文件的NSFileHandle对象
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:srcPath];
//创建用于写入的NSFileHandle对象
NSFileHandle *wrteHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];
//从当前偏移量督导文件的末尾,偏移量默认是起始位置
//NSData *data = [readHandle readDataToEndOfFile]; //下面的写法更好
NSData *data = [readHandle availableData];
//将数据写入目标文件
[wrteHandle writeData:data];
//关闭文件
[readHandle closeFile];
[wrteHandle closeFile];