[英]how to create and write in a text file on specified folder using NeoMAD
I have a requirement to create a text file on specified folder and write the data in it 我需要在指定的文件夹上创建一个文本文件并在其中写入数据
i try this but it doesn't work : 我尝试这样做,但不起作用:
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)
}
Is there any example which will help me? 有什么例子对我有帮助吗?
What do you want to achieve ? 您想实现什么? Serialize a Person object into a file ?
将Person对象序列化为文件?
File and FileOutputStream classes are the same as in JDK, but you need to serialize your data object by yourself, with DataOutputStream for example. File和FileOutputStream类与JDK中的类相同,但是您需要自己自己序列化数据对象,例如,使用DataOutputStream。 Something like this :
像这样的东西:
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.