0

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?

ING
  • 219
  • 1
  • 3
  • 8

1 Answers1

0

What do you want to achieve ? Serialize a Person object into a file ?

File and FileOutputStream classes are the same as in JDK, but you need to serialize your data object by yourself, with DataOutputStream for example. 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();
la_urre
  • 73
  • 6