开发者

File Processing

开发者 https://www.devze.com 2022-12-16 06:49 出处:网络
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, level开发者_StackOverflow社区3 etc with

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, level开发者_StackOverflow社区3 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号