重写writeStreamHeader()以将序列化对象附加到单个文件

时间:2012-08-02 20:38:39

标签: android object serialization append

我知道在这个主题上有几个问题/答案,但我需要更具体的帮助,因为这是我第一次尝试这样的事情。我试图实现这些问题的答案,但仍然有错误。我需要将一定量的序列化对象写入文件,然后从该文件中读取以检索对象。我在android 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
      }

    }

我检查该文件是否存在(因为我运行此代码),然后我的writeObject()在创建“特殊”ObjectOutputStream后使用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 个答案:

答案 0 :(得分:0)

在文件不存在的情况下,您只构建fos。在这两种情况下你都需要它。