2015-02-06 14 views
0

我有一個要求,以創建指定文件夾中的文本文件,並在數據寫入一個文本文件中寫上指定的文件夾它如何創建和使用NeoMAD

我嘗試這一點,但它不工作:

file = new File(FileStorage.getPrivateDir(), "data"); 
    if (!file.exists()) { 
     file.mkdirs(); 
    }else{ 
     fw=new FileOutputStream(file); 
     TextLabel sh=(TextLabel)findView(Res.id.ShFile); 
     Person person; 
     for(int i=0;i<persons.size();i++){ 
      person=(Person) persons.elementAt(i); 
      sh.setText(person.getName()+" "+person.getNumberPhone()); 
      fw.write(Res.id.ShFile); //public void write (int b) 
     } 

有沒有什麼能幫助我的例子?

回答

0

你想實現什麼?將Person對象序列化到文件中?

File和FileOutputStream類與JDK中的相同,但您需要自行序列化數據對象,例如使用DataOutputStream。類似這樣的:

File file = new File(FileStorage.getPrivateDir(), "data"); 
DataOutputStream personStream = new DataOutputStream(new FileOutputStream(file)); 
personStream.writeUTF(person.getName()); 
personStream.writeInt(person.getAge()); 
personStream.flush(); 
personStream.close();