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.FileAPI, such asFile#exists(). It returns aboolean. If it istrue, 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 ofFileOutputStreamtaking a 2nd boolean argument which you set totrue. 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.