开发者

Problems with Serialization

i'm having a few problems with serializing my objects.

I think that i'm missing something, because my application doesn't save the .dat like should be.

Let's show some code :

Load .dat file

public void gravar(ObjectOutputStream out) throws IOException {
      out.writeObject(lista);
      out.writeObject(cadeiras);
      out.writeObject(notas);
      out.close();
  }

Save .dat file

public void carregar(ObjectInputStream in) throws IOException, ClassNotFoundException {
     lista=(ArrayList<String>) in.readObject();
     cadeiras=(ArrayList<String>) in.readObject();
     notas= (ArrayList<String>) in.readObject();
      in.close();开发者_如何转开发
  }

When i try to save the file, my application catch the exception FileNotFoundException here :

case R.id.gravar:
        ObjectOutputStream out;

           try {
                out = new ObjectOutputStream(new FileOutputStream(fich));
                gravar(out);
                   Toast.makeText(getApplicationContext(), "nice!", Toast.LENGTH_LONG).show();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
               Toast.makeText(getApplicationContext(), "error1!", Toast.LENGTH_LONG).show();

                e.printStackTrace();
            } catch (IOException e) {
               Toast.makeText(getApplicationContext(), "error2!", Toast.LENGTH_LONG).show();

                e.printStackTrace();
            }
        return true;

fich is this :

private static String fich = "gravar.dat";

what i'm missing? For better help, i let my code here.

http://pastebin.com/Ax2cHjUA

Thanks in advance!


You should pass the whole path instead of only the filename to FileOutputStream.

If that doing that does not work try

new FileOutputStream(new File(fich));


The solution for this is, instead of

out = new ObjectOutputStream(new FileOutputStream(fich));

paste this

out = new ObjectOutputStream(this.openFileOutput(fich, Context.CONTEXT_IGNORE_SECURITY));

the same for output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜