-1

以下は、私が見つけた4つの関数です(激しいグーグルの後)(私の目的は、作成したオブジェクトのインスタンスを作成し、後でそれらを取得できるようにすることです)

public static Object deserializeObject(byte[] bytes)
{
    // TODO: later read Region object saved in file named by the time stamp during
    // saving.
    // ObjectInputStream inputStream = new ObjectInputStream(new
    // FileInputStream(fileName));

    try {
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object object = in.readObject();
        in.close();

        return object;
      } catch(ClassNotFoundException cnfe) {
        Log.e("deserializeObject", "class not found error", cnfe);

        return null;
      } catch(IOException ioe) {
        Log.e("deserializeObject", "io error", ioe);

        return null;
      }
}


/**
 * Writes content to internal storage making the content private to 
 * the application. The method can be easily changed to take the MODE 
 * as argument and let the caller dictate the visibility: 
 * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc.
 * 
 * @param filename - the name of the file to create
 * @param content - the content to write
 */
public void writeInternalStoragePrivate(
        String filename, byte[] content) {
    try {
        //MODE_PRIVATE creates/replaces a file and makes 
        //  it private to your application. Other modes:
        //    MODE_WORLD_WRITEABLE
        //    MODE_WORLD_READABLE
        //    MODE_APPEND
        FileOutputStream fos = 
           openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(content);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}



/**
 * Reads a file from internal storage
 * @param filename the file to read from
 * @return the file content
 */
public byte[] readInternalStoragePrivate(String filename) {
    int len = 1024;
    byte[] buffer = new byte[len];
    try {
        FileInputStream fis = openFileInput(filename);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int nrb = fis.read(buffer, 0, len); // read up to len bytes
        while (nrb != -1) {
            baos.write(buffer, 0, nrb);
            nrb = fis.read(buffer, 0, len);
        }
        buffer = baos.toByteArray();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

これを使用して、オブジェクト q1.a=12; を読み取ろうとしています。

      byte []q=serializeObject(q1); 
      writeInternalStoragePrivate("shared",q);


byte []w=readInternalStoragePrivate("shared"); 
      fl y=(fl) deserializeObject(w);

(最初の 2 行はシリアル化と書き込み、残りの 2 行は読み取りと逆シリアル化です。)

しかし、アプリをオフにするたびにデータが失われ、0 にリセットされます。

(私は中学生なので、知識はほとんどありません。控えめにしてください)

4

2 に答える 2

0

プロジェクトで同じ問題が発生し、SQLite データベースを使用することになりました。永続性が必要な場合、現在のところ代替手段はありません (DB4O などはすべて機能しません)...

http://sqlite.com/をチェックしてください。SQLite のチュートリアルがたくさんあります。

データベース ソリューション (オブジェクトの取得と挿入) のためにコードを再構築する必要がありますが、残念ながら、実際の代替手段はありません (csv ファイルの保存や同様のアプローチなどの汚い方法を使用しないでください...)。

編集:5つの変数を保存したいだけなら、SharedPreferencesを使用できます。

于 2013-08-09T17:38:29.280 に答える