elis seiti elis seiti - 4 months ago 11
Java Question

How can I write an Object to my raw folder file

This is my serializable class.

public class Person implements Serializable{
private String name;
private String surname;
private int age;


public Person(String name,String surname,int age){
this.name = name;
this.surname = surname;
this.age = age;
}

public void setSurname(String surname) {

this.surname = surname;
}

public void setName(String name) {
this.name = name;
}
public void setAge(int age){
this.age = age;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}

public String getSurname() {
return surname;
}
public String toString(){
return name + " "+surname+" "+age;
}
}


I am trying to write a Person class to a raw folder using FileOutputStream, but nothing is working. Can't create a FileOutputStream, to get the raw folder file.

Also I tried a FileOutputStream.

FileOutputStream fos = getBaseContext().openFileOutput(
"android.resource://com.cpt.sample/raw/text.txt",MODE_PRIVATE);

Answer

You can't change resource file. They are static at run-time. you can check here.

And to put file at runtime you can use :

  • internal storage (or)
  • external storage

and to get this directories use : Environment.getExternalFilesDir()

Fore more information go here.