-2

Need update the file. I see two ways: first - rewrite file (merge content), second - delete previous file and then create new file with new content. I pass content for all file and its weight around 1 KB. What's the way faster?

I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
Nick
  • 117
  • 1
  • 10
  • I think you can check it practically like [this](https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) – Hatik Mar 12 '18 at 07:15
  • The fastest way to do something is *not doing it*. first deleting the existing file is an *additional action* (which BTW is quite expensive since it uses IO to the file system). therefore first deleting the files will be slower. **BUT** never base a performance decision on reasoning. Always proove *by measurement* that your really have a bottleneck and the alternative approach *really solves it*! – Timothy Truckle Mar 12 '18 at 07:20
  • Deleting a file is not expensive. You tell the filesystem "hey, mark this file as deleted" and that's it. But if you're overwriting the file, there's no need to delete it first. Just overwrite it with the new data. – Kayaman Mar 12 '18 at 07:28

2 Answers2

0

There's no need to delete a file before you recreate it. You can just overwrite it with the new content. You can't really "merge" a file, because on the filesystem level blocks will be completely overwritten anyway, even if you just change 1 byte.

If you have the contents you want to end up in the file already, just overwrite the file.

If you have update data that depends on the existing file, read the file into memory (provided it's small enough), merge the data in memory and then overwrite the file. It's not that complicated.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
-1

Not sure practically but in theory merge content shd be faster. Delete n create means - remove pointer for the file, create new one and allocate memory and disk space for it. Merge means allocation is already there it just need expand it. While merging existing file content doesn't need to called in memory on the new one being merged, in creating new file whole lot will be in memory management. Unless there are lots of files OR it's going to make large file over time you won't notice it

user769889
  • 376
  • 2
  • 12