本文共 744 字,大约阅读时间需要 2 分钟。
在开发过程中,需要实现文件的拷贝功能。以下是使用Objective-C编写的文件拷贝示例代码,能够将源文件复制到目标文件路径中。
#importvoid copyFile(NSString *sourcePath, NSString *destinationPath) { // 将文件内容读取并写入目标路径 NSFileHandle *srcFile = [NSFileHandle fileHandleWithPath:sourcePath]; NSFileHandle *dstFile = [NSFileHandle fileHandleWithPath:destinationPath]; if ([srcFile respondsToSelector:@selector(readDataWithLength:)] && [dstFile respondsToSelector:@selector(writeData:from:)]) { // 读取源文件内容 NSData *fileData = [srcFile readDataWithLength:[srcFile length]]; // 将内容写入目标文件 [dstFile writeData:fileData from:srcFile]; [srcFile close]; [dstFile close]; }}
以上代码实现了文件的基本拷贝功能。您可以根据需要传递不同的源文件路径和目标文件路径进行使用。
转载地址:http://bhifk.baihongyu.com/