2011-09-02 127 views
0

我在序列化我的对象时遇到了一些问题。序列化问题

我认为我错过了一些东西,因为我的应用程序没有保存.dat就像应该。

让我们展示一些代码:

负载.dat文件

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

保存.dat文件

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

当我尝试保存文件时,我的应用程序捕获例外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是这样的:

private static String fich = "gravar.dat"; 

什么,我缺少什么?为了获得更好的帮助,我让我的代码在这里。

http://pastebin.com/Ax2cHjUA

提前感谢!

回答

0

的解决方案,这一点,而不是

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

粘贴此

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

相同的输出。

0

你应该通过整个路径而不是只有文件名到FileOutputStream

如果这样做不行尝试

new FileOutputStream(new File(fich)); 
+0

我如何得到整个路径?谢谢 – unpix

+0

我知道答案,我会发布答案(+/- 7小时)。 – unpix