Best method for rewrite/overwrite a file in filesystem using File plugin

Maybe this is a very simple and obvious question, but I didn’t find the answer anywhere and I would like to be sure before implementing it in my app.

The File plugin gives us 2 methods to rewrite a file, at least I could tell. The first is using method createFile() with option replace = true, which will remove the contents of the original file. Then it is possible to write into that file using the method writeExistingFile().

The second option is through method removeFile(), which will remove the file, then it is possible to create a new file with the same name through method writeFile().

I would like to know which of these methods is the most efficient or if there is an even better method that I haven’t mentioned.

Of those two choices, I would use the first simply for atomicity reasons, but in the context of an Ionic app that’s admittedly not a very strong reason. However, my first preference would be Capacitor’s writeFile, allowing eliminating the need for that Cordova plugin in the first place.

1 Like