We want insert something into binary file, but want do I/O as less as possible. We don't want read the second part and write it back. Is there any way to do it. We know in theory, file are saved as blocks in file system. Could we just break the chain of blocks and insert a new one between it?

The same idea could also be used in join two files. Attach one after another. Is there any fast way to do this?

The problem comes from write a large file. We want write it to many small files and join them together.

2 Answers
2

It would involve low-level, file-system specific coding that could possibly mess up the file system.

If this is really, really important for you(r) business, perhaps you could 'emulate' blocks inside a file. This would however result in files of which the intended structure can not be resolved by other 'parties' (other software using the file).

You most certainly don't want to go messing with the file system directly. Let the OS worry about that. If you have to ask this question, you probably don't have the requisite knowledge to manipulate things at that level with greater efficiency that the OS does.

Rather than adding data in the middle of a huge file, consider a different solution that allows you to append data to the end of the file. (Or is some more efficient way). Also, if you're dealing with a huge amount of data, maybe a database would be more efficient than a gargantuan file.

I would be interested in hearing more about what you're trying to do. Perhaps there's an easier way.