0

I know there have been a couple questions asked/answered on this topic but I need more specific help as this is my first time attempting something like this. I have tried to implement the answers to those questions and am still having errors. I need to write a dynamic amount of serialized objects to a file and then read from that file to retrieve the objects. I'm working in android FYI.

Here's my write() and overridden 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
      }

    }

I check to see if that file exists (it does because I've run this code) and then my writeObject() crashes the program with a NULLPointerException after creating the "special" ObjectOutputStream.

Here is my deserializing/read:

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();
                }

Any help is greatly appreciated as I have spent a fair amount of time trying to figure this out. I know there have been people who have got this whole thing to work. As a side note, I have gotten serialize/deserialize working with one object saved to a different file but this is mostly useless given my project requirements.

4

1 回答 1

0

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

于 2012-08-05T09:50:26.473 回答