So what I'm trying to do is have different files to save custom level data so than the player can make their own level, save it and than later on choose which level to load, now I know there's the default FileHandle class, but that only saves a single string and that a problem for me because the way I'm saving it is I'm loading each object into an ArrayList, turning that ArrayList into a JSON string and then saving that string under the rooms ID for that level, so the Preference file would look like
Room ID 1
Json string
Room ID 2
JSON string
Room ID 3
Etc etc
Which is why using FileHandle wouldn't work because all the JSON strings would be saved together and I can't think of a way to separate them
So the question I'm asking is, using Preferences, is there a way I can allow the player to choose which file to load? Because the only way I can think of is to save each preference file's name in another file and loading that, but that's a bit convoluted and doesn't allow the player to load in New files if they get one from a friend for example
Related
I want to save severals files using Stream to serialize objects, with one serializable object per file.
The reason why I want have one file per object is that I have a list of these objects, I am on Android and at the start of the applicaiton I want load all saved objects, this is the easy part.
But during the execution I want add new elements in this list, and delete elements. And I want update the folder with my files in the same time.
So I supposed I have to create one file per object. But may be there is another solution ?
The main problem is : how name each file ? To avoid overwrite a file, ...
I first thinked that I will have to name my files depending the name of a String given by the user, but I will have to check if the name file is valid,...
So may be the solution is to just name my files with an integer, and at the first load, I just continue the counter from the higher file ?
I suppose my problem is frequent, so what are your solutions to write during the execution a dynamic list of objects ?
Thanks you
Here is the thing; I have to store simple data which I have to define once (manually). I must have a functionality to search in it after using keywords. It's like this:
My first item
title:my_title
description:my_description (long, few hundreds words)
keyword1:my_keyword1
keywordx:my_keywordx
And I want a lot of items like this. For example 100 or 1000.
And after in code I want to make search function to look for specific items (as result may be a few, not only one) based on keywords and show the result as text in TextView field for example.
Do You have any idea how I should storage this data? I would prefer .xml file (person who will create data is not a programmer, it'll be much easier for him).
Put your data in JSON format as a text file, create a "res" folder, and put that text file in there. From the "your activity", read this file from the raw folder and parse the JSON.
I am trying to program a game engine in which users would be able to create multiple "level files." However, since the user would be able to make as many levels as they wanted, there's no set amount of levels or set names. I'm not really sure how to import the files in to the engine. I've tried assigning the files to an array but that didn't work well, as it couldn't find any of the methods that way. Currently I have the level files as java files with a method that sets the variables and sets any cutscene text there may be.
Instead of storing Data about user created levels within Java files, a better way to go about this would be to write a Level class which takes input from an external file such as a .txt file.
For example the user can create a file called "firstlevel.txt" and add it to the levels folder which would be able to be read by your Level class.
More specifically your constructor could be something like
Level startingLevel = new Level("startinglvl.txt");
which you could then add to an array of levels
Level[] levels = new Level[64];
and then just add to the array when the game needs to load a user generated level.
I am developing a program which has three JTextBox which my users can enter and check some text for right rule.
So I want add a ablitiy to my program that my users can add or remove their favorite text to a Favorite List and can create folder in Favorite list and put some text in it, such as Bookmark library in FireFox or other web browser.
I want use RandomAccessFile to save favorite list as a favorite source.
How do I implemet it? is there beter way to implement it? is there beter way from RandomAccessFile?
Can any one help me?
Thanks.
There could be lots of approaches. It all depends on what you want to achieve.
Consider using Java serialization mechanism. You can serialize a collection of bookmarks to a file. When your app starts, you deserialize it, and get the same collection data.
The advantages are: simple and easy implementation. The disadvantages: you can't look through stored bookmarks in a text editor or something. The same class hierarchy is to be used to load the serialized version.
XML is human-readable and provides easy interoperability. Other applications would be able to handle your list of bookmarks.
It usually takes more resources to parse the XML and load it to memory and then to create the internal object structures. Though you can use the DOM to traverse the tree all the time, it could be not as convenient as the internal data structure using specialized classes.
Random Access Files work best with fixed record sizes. It means all the fields of your bookmarks must be fixed-length. For example, the name of a bookmark is String. When you write it out to a file, you store it like an array of a fixed length, let's say 20. This automatically implies that if users give a bookmark the name which length is greater than 20, the remaining characters would be lost.
It is also easy to implement with the caveats above. Of course the records could be of variable length, but then you lose the random access to file because you cannot easily calculate the position of a specific record.
Firefox uses JSON for storing bookmarks and allows exporting to HTML. You can explore this too.
You can also store bookmarks, and things you want to keep between sessions in the Preferences,
see http://download.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html
I'm writing a game in which user can create his own level and remove them also.
The only trouble is that I want files to be saved with names as level1, level2, level3 etc without asking user name of level.
While saving a game with level5 name it might be possible that any previous level with that name already exists. Is there any way to avoid such problems.
I mean before saving the name by which i should save should be known previously...
thanx...
You can make use of the methods provided by the java.io.File API, such as File#exists(). It returns a boolean. If it is true, then you can either append to the file, or create another one with a different filename (maybe a counter suffix?). You can append to any existing file using the another constructor of FileOutputStream taking a 2nd boolean argument which you set to true. You can also just stick to using this constructor without checking if the file exists, it will just create new one if file doesn't exist and it will just append if the file exists.
What is the expected behavior in case level5 already exists and the user tries to save level 5? Should the old level5 file be overridden? If not, what else should happen? Should the new file me saved under a different name? And how is your game later on finding this level? If there are multiple level5 files for the 5th level, how shall your game know which one to pick?
Of course you could always create a UUID (that is more or less guaranteed to be unique in practice), create a directory named after that UUID and store the files into the directory as level1 to level5. Next time the user opens the level editor you create a new UUID, thus avoiding any naming conflicts.
Or you can turn it around. You crate a directory name level1, level2, etc. and within each directory you store the files using file names that contain a UUID. That way the game can always easily present a list of all level 5 levels by going into the level5 directory and looking at all files found there.
The question is rather: How will you present those levels to the user when it comes to picking one? As you don't have names, you hardly want to show UUIDs to the user. So I wonder if it is not better to let the user name levels or set of levels (directories).
It seems that your problem is not how but what. Reconsider your goal (desired functional requirements).
If user cannot provide a unique (and meaningful) name for created level, how will he refer to that level (for example in case he wants to edit it)?
I think you should keep additional data for every level like: name (unique), author, maybe date of creation, date of last modification, etc. When user saves (creates or edits) a level he should be warned in case level with provided name exists and should choose between cancel/overwrite/try_another_name. Probably user should be prohibited from overwriting a level when he is not the author.
The best (natural) way to store and process above data is to use a database.