Jupiter
Jupiter

Reputation: 665

Write to file at explicit line in android java

I have an html file in android app public directory that is loaded into a webView with loadData() function. I did not place this html file in directory /android_assetsbecause the file needs to be written to at runtime: why cannot write to /android_assets at runtime

I need to write to the file at runtime by inserting some html snippet at a precise location in the file. My question is: How to write to file at some line? I have read that it may not be safe: Prepending to file in java

Upvotes: 0

Views: 41

Answers (1)

David Conrad
David Conrad

Reputation: 16359

Read the file, write a new copy with the additional information, then either delete the old file and rename the new to the old with File.renameTo or Files.move, or else (perhaps better), update the reference to point to the new file and then delete the old one.

For instance, suppose the file is snippet.1.html, and you refer to this filename based on an entry in a database. You could read the file, and write a modified version as snippet.2.html. Only after that had succeeded, update the database to refer to snippet.2.html. And only after that has succeeded, delete snippet.1.html. Repeat as needed for each time you need to update the file.

Upvotes: 1

Related Questions