读/写一个对象到文件

这是代码:我的任务是序列化我的对象(Person),将其保存在android(私有)文件中,稍后读取文件,(我将得到一个字节数组),并反序列化byta数组。

public void setup() { byte[] data = SerializationUtils.serialize(f); WriteByteToFile(data,filename); } Person p =null ; public void draw() { File te = new File(filename); FileInputStream fin = null; try { fin=new FileInputStream(te); byte filecon[]=new byte[(int)te.length()]; fin.read(filecon); String s = new String(filecon); System.out.println("File content: " + s); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } text(pa,150,150); } 

和我的function:

 public void WriteByteToFile(byte[] mybytes, String filename){ try { FileOutputStream FOS = openFileOutput(filename, MODE_PRIVATE); FOS.write(mybytes); FOS.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done"); } 

它返回一个filenotfoundexception

(我是新来的,所以请耐心和理解)

编辑::这就是我(试图)阅读的方式,(对于我来说)

 ObjectInputStream input = null; String filename = "testFilemost.srl"; try { input = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+filename))); } catch (StreamCorruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Person myPersonObject = (Person) input.readObject(); text(myPersonObject.a,150,150); } catch (OptionalDataException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

和阅读:::

 if(mousePressed) { Person myPersonObject = new Person(); myPersonObject.a=432; String filename = "testFilemost.srl"; ObjectOutput out = null; try { out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+filename)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out.writeObject(myPersonObject); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

您不需要使用’字节数组’方法。 有一种简单的方法来(de)序列化对象。

编辑:这是代码的长版本

读:

 public void read(){ ObjectInputStream input; String filename = "testFilemost.srl"; try { input = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+filename))); Person myPersonObject = (Person) input.readObject(); Log.v("serialization","Person a="+myPersonObject.getA()); input.close(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 

写:

 public void write(){ Person myPersonObject = new Person(); myPersonObject.setA(432); String filename = "testFilemost.srl"; ObjectOutput out = null; try { out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+filename)); out.writeObject(myPersonObject); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 

人员类:

 public class Person implements Serializable { private static final long serialVersionUID = -29238982928391L; int a; public int getA(){ return a; } public void setA(int newA){ a = newA; } } 

创建新的FileOutputStreamFileNotFoundException意味着其中一个中间目录不存在。 尝试

 file.getParentFile().mkdirs(); 

在创建FileOutputStream.之前FileOutputStream.