Take the 2-minute tour ×

I have made a java applet/application game that should save levels that the user can create to their local machine. I say applet/application because it can run either as an application or as an applet. Currently I am using this to write the file:

BufferedWriter bf = new BufferedWriter(new FileWriter("Circles/levels/level.txt"));

lines is an arraylist with the lines to write to the file.


FileReader fr = new FileReader("Circles/levels/level.txt");
        BufferedReader bf = new BufferedReader(fr);
        lines = new ArrayList<String>();
        while (true) {
            String line = bf.readLine();
            if (line == null) {

to read the file. Those are surrounded in try/catch loops, I am just showing what I have to.

This works ok, but it seems to be a completely different location on different operating systems, and the application stores in a different location as the applet.

I would like a way to get an absolute path to a place to save levels that would not change no matter where the java program is being run from, and would be the same if you ran it as an application and as an applet. Also it would be nice to have this work on all operating systems (linux, windows, mac).

share|improve this question
I don't have an answer to your question but a much easier and elegant way to write/read the level to a file would be serialization. Just a tip to make your life easier ;) –  Forgive Goto Jan 1 '13 at 2:36
add comment

1 Answer

up vote 4 down vote accepted

There is no location at all for applets to save data, so if you want to be running as an applet, you have to avoid that, quite simply. The applet sandbox prevents filesystem access completely. You'd need to call on the server to save your data instead.

As for stand-alone applications, I'm also sorry to say that there is no completely generally acceptable convention. However, creating a dotfile directory in the user's home directory is often considered acceptable practice on both Linux, OSX and Windows:

public static final File savedir = new File(new File(System.getProperty("user.home")), ".yourgame");

Then, derive your filenames from that:

FileReader fr = new FileReader(new File(savedir, "level.txt"));

Although it works, it's not exactly what Windows programs would normally do, however.

share|improve this answer
Ok, thank you! Is there a reason for applets not to save data besides convention? Because that script seems to work for both applet and application if I have my .jar file signed. –  DaboRoss Jan 1 '13 at 2:34
It might indeed work if your Jar file is signed. I assumed you didn't sign it since most applets run unsigned (to avoid pestering the user with security dialogs). –  Dolda2000 Jan 1 '13 at 2:37
@DaboRoss - Note your applet won't function as intended if the user doesn't accept the certificate. –  wulfgar.pro Jan 1 '13 at 2:39
Did you mean to say new File( twice? –  DaboRoss Jan 1 '13 at 2:43
I did, but it is admittedly not strictly required. I just prefer, for my own aesthetical reasons, to use that constructor for path concatenation. :) –  Dolda2000 Jan 1 '13 at 2:45
show 2 more comments

Your Answer


