[英]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.