简体   繁体   中英

saving data on sd-card

I'm programming a little game and I want to save on sd-card the scores and the the volume (enabled or disabled) the code of my two functions is:

public static void load(FileIO files) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                files.readFile(".save")));
        soundEnabled = Boolean.parseBoolean(in.readLine());
        for (int i = 0; i < 5; i++) {
            highscores[i] = Integer.parseInt(in.readLine());
        }
    } catch (IOException e) {
        // :( It's ok we have defaults
    } catch (NumberFormatException e) {
        // :/ It's ok, defaults save our day
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
    }
}

//-----------------------

public static void save(FileIO files) {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(
                files.writeFile(".save")));
        out.write(Boolean.toString(soundEnabled));
        for (int i = 0; i < 5; i++) {
            out.write(Integer.toString(highscores[i]));
        }

    } catch (IOException e) {
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }
}

while the program is running this code is ok but if I restart my device the scores are lost.. do you know why?

thanks!!

ps: the FileIO class is:

  public class AndroidFileIO implements FileIO {
    Context context;
    AssetManager assets;
    String externalStoragePath;

    public AndroidFileIO(Context context) {
       this.context = context;
       this.assets = context.getAssets();
       this.externalStoragePath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator;
    }

    public InputStream readAsset(String fileName) throws IOException {
       return assets.open(fileName);
    }

    public InputStream readFile(String fileName) throws IOException {
       return new FileInputStream(externalStoragePath + fileName);
    }

    public OutputStream writeFile(String fileName) throws IOException {
       return new FileOutputStream(externalStoragePath + fileName);
    }

    public SharedPreferences getPreferences() {
       return PreferenceManager.getDefaultSharedPreferences(context);
    }
 }

There are two problems here. First, out.write does not insert a newline at the end of each call, you have to do that manually. So what is happening is when you do the readline in the cal to parse the Boolean you are actually consuming ALL the data in the file. Second, you need to flush and close the file before leaving that function to be sure you do not leave any data in the buffers.

Here is save rewritten that should work:

public static void save(FileIO files) {
BufferedWriter out = null;
try {
    out = new BufferedWriter(new OutputStreamWriter(
            files.writeFile(".mrnom")));
    out.write(Boolean.toString(soundEnabled));
    out.write("\n");
    for (int i = 0; i < 5; i++) {
        out.write(Integer.toString(highscores[i]));
        out.write("\n");

    }
    out.flush();
    out.close();
} catch (IOException e) {
} finally {
    try {
        if (out != null)
            out.close();
    } catch (IOException e) {

    }
}
}

I'm proggraming for first time but iv solved this using shared prefs. That way you avoid losing data when updating the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM