Alex Newton
Alex Newton

Reputation: 163

Android - Best way to edit .txt file?

I've had a look around and haven't been able to come up with an answer to my issue.

I'm creating a fitness app and it allows users to save different workouts for future use. All the information is saved in txt file on internal storage. I'm trying to now implement a feature to be able to edit these workouts, so my question is:

What is the best way to edit a .txt file in android? Should I just delete the old entry and save the new one in its place or is there a better way?

Upvotes: 2

Views: 3375

Answers (2)

wsgh
wsgh

Reputation: 235

Using SQLite database maybe the best way of saving data for different users. And if you want to edit a text file, you can load the full file in memory and rewrite the file after modify the content.

Upvotes: 0

Elemental
Elemental

Reputation: 7466

You can only append to a text file (add to the end); any other edit requires that you load the full file in working memory, modify it, and save it a new file (possibly overwriting the old one).

If this sounds like a bad idea (because the files are large and complex) then perhaps you should be looking at using SQLLite facilities which are standard android libraries and designed relatively simple record keeping tasks of this nature.

Unless your data is extremely unusual the SQL path will make for easier, clearer code in the long term.

Upvotes: 1

Related Questions