[英]how to create and write in a text file on specified folder using 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)
}
有什么例子對我有幫助嗎?
您想實現什么? 將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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.