Android Open Source - game-api-android Game Persistence






From Project

Back to project page game-api-android.

License

The source code is released under:

MIT License

If you think the Android project game-api-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.gameengine.icadroids.persistence;
/*ww w  .jav a2s .  c  o m*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.gameengine.icadroids.engine.GameEngine;

/**
 * Class for user persistence. With this Class you can save Strings in a File to
 * your internal storage. Both real devices and emulators can use the internal
 * storage.
 * 
 * You can use the game persistance for example to save variables or,
 * highscores. You are allowed to use multiple game persistances, each
 * additional persistance saves an additional file on your system if the
 * filename name is different.
 * 
 * @author Roel
 */
public class GamePersistence {

  private String filename;

  /**
   * The constructor allows you to specify the filename the internal storage
   * will use.
   * 
   * @param filename
   *            The name of the file that will be used for this persistance.
   */
  public GamePersistence(String filename) {
    this.filename = filename;
  }

  /**
   * Saves the data to the earlier specified file. All previous saved data
   * will be overwritten. You can get your previous data by loading it first
   * and then appending your new data to the String.
   * 
   * @param data
   *            The data that should be saved to the file, the data must be
   *            one entire String.
   */
  public void saveData(String data) {
    FileOutputStream fos;
    try {
      GameEngine.getAppContext();
      fos = GameEngine.getAppContext().openFileOutput(filename,
          Context.MODE_PRIVATE);
      OutputStreamWriter osw = new OutputStreamWriter(fos);
      osw.write(data);
      osw.flush();
      osw.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Returns the full data (in String format) that is stored in the file. This
   * function will return the data or an empty String if the file does not
   * exist, or the file is empty.
   * 
   * @return String The data saved earlier.
   */
  public String loadData() {
    String content = "";
    File file = new File(GameEngine.getAppContext().getFilesDir(), filename);
    if (file != null) {
      int buffersize = (int) file.length();
      char[] buffer = new char[buffersize];
      try {
        FileInputStream fIn = GameEngine.getAppContext().openFileInput(
            filename);
        InputStreamReader isr = new InputStreamReader(fIn);
        isr.read(buffer);
        isr.close();
        fIn.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      content = new String(buffer);
    }

    return content;
  }

}




Java Source Code List

android.gameengine.icadroids.alarms.Alarm.java
android.gameengine.icadroids.alarms.IAlarm.java
android.gameengine.icadroids.dashboard.DashboardImageView.java
android.gameengine.icadroids.dashboard.DashboardTextView.java
android.gameengine.icadroids.engine.GameEngine.java
android.gameengine.icadroids.engine.GameFPSCounter.java
android.gameengine.icadroids.engine.GameThread.java
android.gameengine.icadroids.engine.GameView.java
android.gameengine.icadroids.engine.Viewport.java
android.gameengine.icadroids.forms.GameForm.java
android.gameengine.icadroids.forms.IFormInput.java
android.gameengine.icadroids.forms.ViewCreator.java
android.gameengine.icadroids.forms.ViewRemover.java
android.gameengine.icadroids.input.MotionSensor.java
android.gameengine.icadroids.input.OnScreenButton.java
android.gameengine.icadroids.input.OnScreenButtons.java
android.gameengine.icadroids.input.TouchInput.java
android.gameengine.icadroids.objects.GameObject.java
android.gameengine.icadroids.objects.MoveableGameObject.java
android.gameengine.icadroids.objects.collisions.CollidingObject.java
android.gameengine.icadroids.objects.collisions.ICollision.java
android.gameengine.icadroids.objects.collisions.TileCollision.java
android.gameengine.icadroids.objects.graphics.AnimatedSprite.java
android.gameengine.icadroids.objects.graphics.Sprite.java
android.gameengine.icadroids.persistence.GamePersistence.java
android.gameengine.icadroids.sound.GameSound.java
android.gameengine.icadroids.sound.MusicPlayer.java
android.gameengine.icadroids.tiles.GameTiles.java
android.gameengine.icadroids.tiles.Tile.java
com.android.vissenspel.Monster.java
com.android.vissenspel.StrawberryControler.java
com.android.vissenspel.Strawberry.java
com.android.vissenspel.Vis.java
com.android.vissenspel.Vissenkom.java
game.MyAndroidGame.java
testGames.AndroidCraft_demo.java
testGames.FormTest.java
testGames.Player.java
testGames.TestGameBas.java
testGames.TestGameLex.java
testGames.TestGameRoel.java
testGames.gameEngineTest.DebugEngine.java
testGames.gameEngineTest.GameEngineTestGame.java
testGames.gameEngineTest.TestGameObject.java
testGames.gameEngineTest.debugObject.java
testGames.gameEngineTest.randomObject.java
testGames.testGameObjectBas.java
testGames.testGame.java