I have a json file with a lot of objects. This file is located in res/raw. Im reading this file and showing objects in list in my UI. And after user closing activity with this list, changes that user made most be saved in file that was read.
If your file is located inside of your apk file, you cannot change it. Instead of that have a flag stored in persistent store. If it is a first run of your application, then read this json file from apk, process it and store it in the device filesystem.
And every next run, check the flag, if it is not the first run, read the file from the filesystem and process it further.
Here is the bad way of making things static, but if too urgent for you, you can read json file once and convert it to java model(pojo) object and make that object static, When next time you visit same activity load ui from that static object instead of reading and parsing it again, for safety you can check if pojo class object is null.
Related
I'm working on a game and I am at that part that I want to save the game progress into a text file (or maybe a properties file would be good too), but I'd like to save that file to a place that is not reachable for the players. I was thinking about saving it to a source folder inside the program, but I am not able to save or load a text file from there, only images. Could anybody suggest something how/where to save the game stats that players can not just go into the settings file and modify their score or level or something like that?
With the properties file my only problem is the saving, where I need an output stream or a writer to save it, what I', not able to get
Assuming your player's pc has a regular hard drive with regular standards then:
If the game is offline(and maybe later synced with a server) then the answer is NO; if your app can access the file so does the user; even if you encrypt the data written to the save file people can just reverse engineer your app and get the keys and algorithms and modify the file; to put it simply you can only complicate it, their access to save files is inevitable.
If your game is only possible to be played online then you can do sanity check for every action of the players and save the progress in an inaccessible by players manner;
About the read only, been asked before:
create a read-only file
For the second one, keep in mind that as you know the path to the file and the name of it, the user don't. Saving the file using "scary" name, in un-trivial path will protect the file from any changes for a while
I'm working on an application which is coded in Java. It reads calibration values for later calculations from .xml file on first initialization. Values can be later modified and saved to an xml file from another form called 'Properties', that is opened from the Main form.
How can I notify the Main form after the 'Properties' form is closed to read new modified values from .xml file? Or is there a better way to exchange this data and I should not read .xml again after?
Without more information about how your forms are set up it's hard to give a true answer.
Create an object set up to hold the information you will read from the .xml file. In Main, have one (or more as necessary) of these objects. Store the information from the .xml file into it. Pass and update that data object in Properties form, and have it be returned. After you're done editing, fire a propertyChange or call a method which will trigger tell Main to read from the data store object.
If you want to stick with re-reading the .xml file upon closing the Properties form, then you would need to edit the window/form closing procedure (depends on how your form is set up). If the Properties form is a JFrame, then there should be a "windowClosing" method that can be written for it.
If I had a directory filled with different object files, is there a way I could input them into my application without opening a new stream every time? I am currently using ObjectInputStream, but I don't mind using another form of IO.
For example, if I stored my users directly onto my harddrive as objects (each having their own file: name.user), is there a way I could load them all back in using the same stream? Or would it be impossible seeing how a new File object would be needed for each individual file? Is there a way around this?
Each file will need its own stream behind the scenes; there's no way round that. But that doesn't stop you creating your own InputStream that manages this for you, and then allows you to read everything off from one stream.
The idea would be that when you try to read from your CompoundObjectInputStream or whatever, it looks to see if there are any more files that it hasn't yet processed, and opens one if so using another stream, and passes the data through. When it reaches the point where there are no more files in that directory, the CompoundObjectInputStream indicates end-of-stream.
No, there is not. Each physical file requires its own FileInputStream, FileChannel, or other corresponding native accessor.
Note that File has no direct link to a physical file, it is just an abstract path name.
I am considering using the following code:
comboBox.addItem(...);
If I continue using this code to add items to the comboBox, would it be possible to save the items into either a text file or memory? *so that it can be loaded in the next time the user wants to use the gui.
Thanks for any help you may provide. :)
Yes, I would recommend creating a class that serializes the object to a text file. This can be accomplished with Input/Output Streams or the File class.
So you would take the array of objects, then serialize the object to a text file, then read it back into the array the next time the program runs.
Here is some info on Serializable: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
File Class: http://docs.oracle.com/javase/7/docs/api/java/io/File.html
I have a dialog box that the user inserts various data through gui controls,
and this information is saved as an xml file (implemented in java).
The information stored in the xml file is configuration information
for the application.
I can manually modify the xml configuration file, but I also want to provide this capability through a UI as well.
So when the dialog is opened (for creation of configuration) a corresponding well-defined object is populated by the various values input by the user.
Once the user presses 'save' the information in the object is stored as xml.
Now I was thinking to provide the option for edit the file via UI. So the same dialog is displayed to the user, but this time with the configuration information already filled-in by the loaded file. The corresponding object is populated as well.
I am not sure what is the best way to modify the file at this point.
Should I use 2 objects, 1 that stores all the file's info and 1 that stores the modified values from the dialog, and start comparing the objects for changes so that I modify the file? Or should I delete the file and create a new one?
Which is the best approach, and how would I proceed in each?
Thank you
Consider the data flow. The user will work with the GUI and make changes. The moment they make a modification, the data on the GUI is out of step with the XML. If the user opts to save the data then a simple marshalling operation (trivial if using JAXB) will ensure that the XML is updated. You don't need to compare every field, there's no point - of course you have validated the contents prior to committing them to file. If the user opts to cancel then no marshalling takes place.
There is no need to make a backup and no need to compare what is already in the XML.
However, if the user needs to be able to undo a save (such as revert to previous configuration) then you'll need a backup structure (or maintain a stack of GUI models in memory). I would not recommend that approach, though, as you're just chucking in needless complexity. Users are typically happy with a Save or Cancel button and no Revert.
Keep it simple. Just overwrite the entire file using the updated object. Then you won't need any special code for each case. All you will need is one method to marshall the object into the file and one method to un-marshall it.