0

このトピックに関していくつかの質問/回答があったことは承知していますが、このようなことを試みるのはこれが初めてなので、より具体的なサポートが必要です。これらの質問に対する回答を実装しようとしましたが、まだエラーが発生しています。シリアル化されたオブジェクトを動的にファイルに書き込み、そのファイルから読み取ってオブジェクトを取得する必要があります。私はアンドロイドFYIで働いています。

これが私の write() とオーバーライドされた writeStreamHeader() です:

//check and see if there is a file already created to hold patterns, if not, make one
//only created once or if file is deleted; append to it if it's already created
    if(!new File(getFilesDir()+"/Patterns").exists())
        {
            try {
                fos = new FileOutputStream(getFilesDir()+FILENAME, true);
                out = new ObjectOutputStream(fos);
                 } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
        else
        {
        try {
            out = new AppendingObjectOutputStream(fos);
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
         }

        //create a pattern of points. Max amount of Patterns is 100.
                    if(patternindex!=100)
                    { 
                        Patterns[patternindex] = new Pattern(ActivePoints, name, xshift, yshift, scaling, rotation);
                        try {
                            //out.reset();
                            out.writeObject(Patterns[patternindex]); //write the object
                            //out.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        patternindex++;
                        dialog.dismiss();
                    }

public class AppendingObjectOutputStream extends ObjectOutputStream {

      public AppendingObjectOutputStream(OutputStream out) throws IOException {
        super(out);
      }
      @Override
      protected void writeStreamHeader() throws IOException {
          out.reset();
        // do not write a header
      }

    }

そのファイルが存在するかどうかを確認し (このコードを実行したため存在します)、「特別な」ObjectOutputStream を作成した後、writeObject() が NULLPointerException でプログラムをクラッシュさせます。

ここに私の逆シリアル化/読み取りがあります:

String PatternNames[] = new String[2];
Pattern Patterns[] = new Pattern[2];
FileInputStream fis;
ObjectInputStream in;

try {
            fis = new FileInputStream(getFilesDir()+"/Patterns");
            in = new ObjectInputStream(fis);

            for(int i=0;i<2;i++)//just trying to read 2 objects to start with
            {
                {
                    Patterns[i] = (Pattern) in.readObject();
                    PatternNames[i] = Patterns[i].getName();
                }


            }
            in.close();
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
                }

これを理解するためにかなりの時間を費やしたので、どんな助けも大歓迎です。このすべてを機能させた人がいることを私は知っています。補足として、別のファイルに保存された 1 つのオブジェクトをシリアライズ/デシリアライズする作業を行いましたが、プロジェクトの要件を考えると、これはほとんど役に立ちません。

4

1 に答える 1

0

fosファイルが存在しない場合にのみ構築しています。どちらの場合も必要です。

于 2012-08-05T09:50:26.473 に答える