Android - Best way to edit .txt file? - java

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?

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.

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.

Related

What to use : Excel VS MySql/MongoDB [Java]

I am going to make a business application for my father to make GST(Goods and Services Tax) filing easier. I have the design ready and I am going to use JavaFX.
The user will enter the data in tableview and that data needs to be stored for future reference.
The tableview needs to be converted to an excel file (gonna use Apache POI). The excel file will be sent to a C.A who will file GST on my father's behalf.
The application will need to import/export data into/from the tableview and edit the data as necessary.
I have 2 options :
Store/retrieve data from MySQL to tableview, update it according to the user's will and later export the data into excel files for sending it to C.A.
Store/retrieve data from excel files to tableview, update it according to the user's will and send the excel file to C.A.
I am planning to expand the application into a complete Business software that can manage entire business.
What should I use?
Which one will be more efficient and why?
I hope I am able to convey my question (I ain't good at writing).
In my own opinion it is more efficient and have more posibilities to explotes the data using MySQL, because reading and writing an Excel file will take a lot of time and it is slower.
I'll answer my own question, since I have got the answer.
I'm going with SQLite for now as using csv or excel files is gonna consume a lot of resources (I tried it).
I am going to sync the .db file in drive using scripts from the application itself. MySQL is definitely better choice but I want to database to be used by 2 computers at a time (not in network) so I will have to pay for online database.
I will store the .db file and drive and will retrieve it whenever the application runs. In this way its going to be safe.

Android's Backup Manager to backup a minimal file

is it possible to use
Android's Backup Manager
to backup a minimal file
(A text file with 20 words?)
I can't quite understand if this is possible and how to do it. ..
As you can read at http://developer.android.com/guide/topics/data/backup.html you can save any type of data, including files.
Also take a look at http://developer.android.com/reference/android/app/backup/FileBackupHelper.html
If you are using the original backup APIs, you probably just want your BackupAgent implementation to extend android.app.backup.BackupAgentHelper, and then use a FileBackupHelper configured for the file you want to keep backed up.
https://android.googlesource.com/platform/development/+/master/samples/BackupRestore/src/com/example/android/backuprestore/FileHelperExampleAgent.java is an example of using FileBackupHelper for a single file. There are other example agents in that same sample code directory that show other alternatives for keeping data backed up.
On Android 6.0 (Marshmallow) and later, there is a new file-based backup API you can use instead that requires much less code in your application, possibly none at all. Take a look at http://developer.android.com/training/backup/autosyncapi.html for an overview of how you can use this new facility to get automatic backup of a file without having to actually write your own backup agent.

notepad eclipse java android internal storage

I am trying to do an eclipse java android app using internal storage.
It is a notepad. What it should be able to do is write and save notes, search for them an edit them.
the most similar application of what I need is this:
http://developer.android.com/training/notepad/notepad-ex3.html
but I dont know how to modify if to use internal storage and not external, i also found this file that allows you to read and save in internal storage
http://www.androidaspect.com/2013/09/android-internal-storage-tutorial.html
but now i dont know how change the database for the inernal storage
Okay let's break it down. You want to make something that will let you take notes. Fair enough. You need the kind of storage that will make retrieval and insertion easier.
Let's start with a single text file that you store somewhere (internal or external). If you use this approach, inserting and deleting data becomes difficult because any changes will require you to make changes to the actual text. Not to mention, you will need to format your notes in a way so that you can differentiate one note from another.
Okay, so will using multiple text files solve this? In a way, yes, but you can go for a better approach using SQLite.
SQLiteDatabase has methods to create, delete, execute SQL commands,
and perform other common database management tasks.
SQLite seems like the prime candidate for your storage because it makes CRUD (Create, Read, Update, Delete) easier. Instead of trying to modify the code, start from scratch.
Have a look at this tutorial https://www.youtube.com/watch?v=j-IV87qQ00M

Java - Integrity of txt documents

Within a Java program i've got a bunch of text files which the program reads and writes to (i know this is a really bad way to implement an app) but I need some way to ensure the integrity of the text files every time the program loads.
If the text file is deleted the program will be able to re-create it as it was last. Is there any way of doing something like this where I can store data between program executions? - But the important thing is that i'm able to change the data stored.
(Usually would use a database but it's not an option atm).
edit: (Clarify what I'm looking for)
There exists a text file full of data.
User deletes the text file.
Program detects wrong or missing file and re-creates it from a backup which the user can't get his hands on.
This is the kind of process i'm trying to implement.
You can't save data locally in a safe way. Everything that is stored on the users machine is under the users control. You can make them jump through hoops, like with using encryption or storing files in obscure formats in strange places, but you will just make it less convenient to change the files, not impossible for a determined user.
The only way to get around this is to store the data online.

Resaving A Blob File In Java

I have a web application in GWT and a complementary desktop client also written in Java (so the same solution basically applies to both). In my program users can attach files, then download them later or do whatever. These files are stored as blobs and can be in just about any format. Many of the users that use Excel and Word want to be able to open the file, make changes, then have those changes stored back in the attached file. In other words, need an inline editing of attachments.
Any ideas on how to make this happen? Should I have an 'edit' mode that keeps a file handler while the file is open, and then store that File handler? Some way keeping track of whether the file is changing, or not?
Sorry about the late response. Amol >> I have that going. I want to save directly back to a blob as if it were a filehandle. Thought that was clear in my question.
I have decided that this is almost impossible with a web application without writing some kind of client interface for each and every potential file type - word, excel, pdf, graphics, etc...

Categories