Android Open Source - OD-PlayerStats File I O






From Project

Back to project page OD-PlayerStats.

License

The source code is released under:

MIT License

If you think the Android project OD-PlayerStats 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 net.black_pixel.od_playerstats.fileIO;
/* w  ww .j ava  2 s .  c om*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Map;
import java.util.TreeMap;
import android.content.Context;
import android.util.Log;

public class FileIO {
  Map<String, String> savedNames;
  Context context;

  public FileIO(Context context) {
    this.context = context;
    savedNames = loadNames();
  }

  public Map<String, String> getNames() {
    return savedNames;
  }

  private Map<String, String> loadNames() {
    FileInputStream fis = null;
    try {
      fis = context.openFileInput("savedNames");
    } catch (FileNotFoundException e1) {
      savedNames = new TreeMap<String, String>();
      saveNames(savedNames);
      e1.printStackTrace();
      return savedNames;
    }

    ObjectInputStream inputStream;
    try {
      inputStream = new ObjectInputStream(fis);
      savedNames = (TreeMap<String, String>) inputStream.readObject();
      inputStream.close();
    } catch (StreamCorruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return savedNames;
  }

  public void saveNames(Map<String, String> savedNames) {
    this.savedNames = savedNames;

    FileOutputStream fos = null;
    try {
      fos = context.openFileOutput("savedNames", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
      // this should never happen because file would be created if not
      // found
    }
    ObjectOutputStream outputStream;
    try {
      outputStream = new ObjectOutputStream(fos);
      outputStream.writeObject(savedNames);
      outputStream.flush();
      outputStream.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}




Java Source Code List

net.black_pixel.od_playerstats.MainActivity.java
net.black_pixel.od_playerstats.fileIO.FileIO.java
net.black_pixel.od_playerstats.parser.Parser.java