简体   繁体   English

如何将文件中特定偏移量的字节复制到特定偏移量的另一个文件?

[英]How to copy bytes from a specific offset in a file to another file at a specific offset?

Would like the most efficient way of copying, say, 1MB from an offset in file 1 to the same or different offset in file 2. Is it possible to have multiple threads doing the reads and the writes at the same time safely? 想要从文件1中的偏移量到文件2中的相同或不同偏移量的最有效方式复制,例如1MB。是否可以让多个线程安全地同时执行读取和写入操作?

To clarify, I of course want to handle file->file as stated. 为了澄清,我当然希望按照规定处理文件 - >文件。 However I also want to have multiple threads reading from a network IO bound location (internet, etc..), and then reassemble those back into a single file locally. 但是,我还希望从网络IO绑定位置(Internet等等)读取多个线程,然后将它们重新组装回本地的单个文件中。 If it makes more sense that the write operation be single-threaded that is fine too. 如果更有意义的是写操作是单线程的,那也很好。

You almost certainly don't want multiple threads doing this - you're going to be IO-bound, and you don't want the overhead of the disk seeking all over the place. 你几乎肯定不希望多个线程这样做 - 你将成为IO绑定,并且你不希望磁盘的开销在各地寻找。

I'd just do it in one thread: 我只是在一个线程中做到这一点:

  • Open file 1 for reading 打开文件1进行阅读
  • Seek 寻求
  • Open file 2 for writing 打开文件2进行写作
  • Seek 寻求
  • Repeatedly copy a buffer at a time (eg 32K) from one stream to the other. 从一个流到另一个流一次(例如32K)重复复制缓冲区。

You may find that some of the FileOptions (eg SequentialScan for the reader) could make a difference. 您可能会发现某些FileOptions (例如读者的SequentialScan )可能会有所不同。

EDIT: As noted in comments, it may be worth using two threads - one to read and one to write, particularly if you're using two separate drives. 编辑:如评论中所述, 可能值得使用两个线程 - 一个用于读取,一个用于写入,特别是如果您使用两个单独的驱动器。 However, it's also possible that with the operating system automatically doing prefetching etc, that wouldn't be helpful. 但是,操作系统可以自动进行预取等操作,这样做也没有用。 It would certainly complicate the code. 肯定会使代码复杂化。

Do you have a target time for this operation? 你有这个手术的目标时间吗? How fast does a simple implementation take compared with that target time? 与目标时间相比, 简单实现的速度有多快? I definitely wouldn't venture into multiple threads or async operations until you've established how long the simple approach takes. 在确定简单方法需要多长时间之前,我绝对不会冒险进入多线程或异步操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM