把文件A的内容拷贝到文件B的末尾

//假设文件目录下已经存在fileA和fileB两个文件
//把文件A的内容添加到文件B的末尾
#import 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSFileHandle *originFile, *destinationFile;
        NSData *buffer;
        //打开文件进行读取操作
        originFile = [NSFileHandle fileHandleForReadingAtPath:@"fileA.txt"];
        //判断文件是否打开读取成功
        if (!originFile) {
            NSLog(@"Open of originFile for reading failed.");
            return 1;
        }else{
            NSLog(@"Open of originFile for reading successful.");
        }
        //打开要更新的文件
        destinationFile = [NSFileHandle fileHandleForWritingAtPath:@"FileB.txt"];
        //判断文件打开写入是否成功
        if (!destinationFile) {
            NSLog(@"Open of originFile for writing failed.");
            return 2;
        }else{
            NSLog(@"Open of destinationFile for writing successful.");
        }
        //在文件的末尾进行查找
        [destinationFile seekToEndOfFile];
        //读取写入数据
        buffer = [originFile readDataToEndOfFile];
        [destinationFile writeData:buffer];
        //关闭文件
        [originFile closeFile];
        [destinationFile closeFile];
        //读取打印文件内容
        NSLog(@"%@",[NSString stringWithContentsOfFile:@"FileB.txt" encoding:NSUTF8StringEncoding error:NULL]);
    }
    return 0;
}

你可能感兴趣的:(把文件A的内容拷贝到文件B的末尾)