I'm currently writing an application which displays items within a ListView which then users can either add to or delete from. I am making this data persistent by writing out to a local JSON file (I'm saving the data as a JSONArray and then calling the toString() method before Serializing it and writing it, reading is basically the same process reversed).
Since I'm fairly new to developing for Android I'm not very clear on the best practices, and specifically I'm interested in what is the best method for accessing data stored within local files and resource files. Currently I'm Reading and writing from these using an AsyncTaskLoader and am wondering if that's sensible for a local file or is not needed/recommended for local files. Is it OK to run on the main thread for accessing local files or is there some other class besides AsyncTaskLoader I can use?
My main problem is that after implementing the AsyncTaskLoader I am seeing some performance issues and being fairly new to development as a whole I suspect I'm not implementing this fairly complex class in an efficient way. If I can avoid it all together or substitute a less complex class to reach a similar result I think I'd be set. Thanks in advance for feedback and if having some code snippets would help clarify the question please let me know.
After searching around for a bit and trying a few different things I found that implementing a SQLLight Database and Cursor Loader was alot faster than trying to Serialize and De-serialize strings to JSON and vice-versa. Also when everything was setup it was a lot easier to implement.
This project is a pretty good example of how to implement this https://github.com/udacity/ud845-Pets .
If your Data file is to big to read and write then you need to use some Thread or AsyncTask. AsyncTask has mainly three methods for batter performance with android UI .
for storing data you can store your json String into SharedPreferences.
see below links for
AsyncTask
And
SharedPreferences
Related
For my Java project I am looking for a convenient way to store my data. I have the following requirements:
It should be easy to synchronize with subversion (which I use for my Java code and other stuff). So I guess file-based is appropriate.
I want to be able to get certain elements without having to read all data into memory. Like in a database ("give me all objects with/without property x", "give me all information about object with certain ID").
I want to be able to read and write in this way.
I guess a database is overkill for my purpose, difficult to sync and I have to be admin/root on all machines to install it. (right?)
So I was thinking of using XML, but I heard that XML parsing in Java does not work very well. Or can anyone point me to a good library?
Then I was thinking of CSV. But all examples I saw (here and elsewhere) read the data into memory before processing it, which is not what I want.
I hope you can help me with this problem, because I am not so experienced with Java.
Edit:
Thank you for downvoting this question without any comment. This is not helpful at all because now I have no new information on my problem and I also have no idea what I did wrong with respect to this community's rules.
You can use Datanucleus (ORM) and use it with an XML Datastore
http://www.datanucleus.org/products/datanucleus/datastores/xml.html
I am inexperienced with Spring and I've been reading up on persistence options in Spring, as I am trying to find a suitable way to store data without the use of a database such as Oracle or MySQL etc...
When my app loads, it will read a file containing IDs. As the app runs, it may gain new IDs which will need to be written to the file in case of a crash. From what I can tell, I will need to replace the whole file each time, which is fine, as the data should be held in RAM and I can just overwrite the original file.
What I would prefer, however, is a way in Spring, or even Java, to sync the file and the data so that if I add 1 new ID to my list, it would automatically add a single line to the end of the file without me needing to write additional file management code. I know I can probably just concatenate the line, but something that basic probably won't be thread safe, and thread safety is a major concern here. I'd rather find a ready-made lib rather than re-invent the wheel.
So, can anyone point me in the direction of a tutorial, or technology, that allows for what I need? Or tell me if one exists, or how best I should go about this?
Thanks.
EDIT: It seems Springs resource bundle is the way forward. But I don't think it does exactly what I need to do. Using this, I will have to write code to both add to the map, and then add to the file.
Take a look of SQLite
Is a thread safe and server less sql database with Java driver.
EDIT
Other option is spring batch support for flat files.
see http://docs.spring.io/spring-batch/reference/html/readersAndWriters.html#flatfiles
I have a program where I need to save a running application to be able to go back to it later
I know that I can write/read from a text file to achieve this but the program is pretty prodigious so it's not really a good way to do it because I have 10+ classes and thousands of JTextFields, JComboBoxs, etc. Does anyone know of a way I can achieve this without writing/reading from text files?
An example of what I need to be able to do is this:
In Microsoft Excel you can load files (.exl) into it and be able to edit them.
The Swing Application Framework provides a way to save session state when your application exits and restore the state when you restart. Session state is the graphical window configuration of your application. This state includes window size, internal frame locations, selected tabs, column widths, and other graphical properties.
How do you think Excel does this? It stores the type and value of each cell, along with metadata describing the worksheet in its own proprietary binary format in a file. If you have a custom application with complex internal state, you will have to design a storage format and serialize the state yourself. You may be able to use Java Serialization, but not without some effort.
A good way to do this is to save the data from your controls into a canonical form and then make that class serializable. You can then persist that data to a file. Here's a link about serialization in Java.
UPDATE
I just noticed that you said you have thousands of form controls. So you probably don't want to do all of this in one class, but you probably want to maintain a hierarchy of classes and split out the data into separate classes. This will also help you separate your concerns. Hopefully you have POJOs or domain classes that represents your data. If that is the case, your task will be much easier. This is also why separating concerns is good :).
To save the state of an application, i can think of two popular way:
1) save the state of the application in a Database
2) save the state of the application in a binary file or XML,json or any format you want.
Maybe giving more details about the app. would help.
is it a Web app, fat client app, client/server app... !^
Solution may vary with the type of application.
Hope it help.
I have my most of my apps "dynamic" data stored in the datastore.
However, I also have a large collection of static data that would only change with new builds of the app. A series of flat files seems like it might be simpler than managing it in the datastore.
Are there standard solutions to this? How about libraries to make loading/parsing this content quick and easy? Does it make more sense to push this data to the datastore? Which would perform better?
Anyone else have this problem and have war stories they can share?
Everything depends on how you need to use the information.
I for instance have an application that needs to have a starting state provided from static data. Since I wanted this static data to be easily prepared outside the application, I put the data as spreadsheets on Google Docs and then I have an administrative function in my web app to load the starting state through Google Docs Spreadsheet API to objects in the datastore. It works fairly well, although there are some reliability issues that I haven't quite worked out yet (I sometimes need to restart the process).
In other cases, you might get away with just including the data as static property/xml files and load them through the standard Java resource APIs (getResourceAsStream and such). I haven't tried this approach though since it wasn't meaningful in my particular situation.
Basically, I am looking for a simple way to list and access a set of strings in stream form in an abstract manner. The only issue is that Java's file-accessing API can only be used for listing and reading files, and any sort of non-filesystem storage of the data uses a different API. My question is whether there is some common API I could use (whether included in Java or as an external API) so that I could access both in an abstract manner, but also somewhat efficiently.
Essentially I want a set of lazily streamed text files. Something like Set might be reasonable, except on a filesystem, you would have to open the text streams even if you don't end up wanting to access that file.
Some sort of api like
String[] TextStorage.list()
InputStream TextStorage.open(String elementname);
which could abstractly be used to access either filesystems or databases, or some other storage mechanism I invent in the future (maybe fetching something across the internet).
Is there a library which already does this? Can I do this with the already existing Java API? Do I need to write this myself? I'd be surprised if no-one has encountered this problem before, but my google-fu and stackoverflow searches don't seem to find anything.
you might use HSQL
http://hsqldb.org/