I'm trying to store an object to a data file, and I can create the file, but when I try append anything to the file it simply creates a new file and overwrites the old file.
My create code:
public void createObject(Object object)
{
//File outFile = new File(Environment.getExternalStorageDirectory(), "foobar.data");
try
{
File outFile = new File(Environment.getExternalStorageDirectory(), "foobar.data");
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(object);
out.close();
}
catch (IOException e)
{
Log.i("CreateObject", "Write - Catch error can't find .data");
}
finally
{
try
{
// out.close();
Log.i("CreateObject", "Closed successfully!");
}
catch (Exception e)
{
Log.i("CreateObject", "Write - Failed to close object output stream.");
}
}
I tried using the code at https://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html and replaced my try
with
FileOutputStream fos = new FileOutputStream("foobar.data");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(event);
oos.close();
but my program goes straight to catch
. The catch error is java.io.FileNotFoundException: /foobar.data: open failed: EROFS (Read-only file system)