I'm working on an Aplication and I want to have an ArrayList in a file. I wrote the two methods to save and get :
public static boolean saveMetars(Context context, ArrayList<Metar> metars) {
try {
FileOutputStream fos = context.openFileOutput("metars_array", Context.MODE_WORLD_READABLE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(metars);
oos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static ArrayList<Metar> getArrayMetars(Context context) {
try {
Log.d("here", "asas");
FileInputStream fis = context.openFileInput("metars_array");
ObjectInputStream is = new ObjectInputStream(fis);
Object readObject = is.readObject();
is.close();
if(readObject != null && readObject instanceof ArrayList) {
Log.d("here2", "asas");
return (ArrayList<Metar>) readObject;
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
My Problem is: When the user installs the application and uses it for the first time, I have a xml file, and I use one method (created in my appi) to read it and create Metar Objects to add to my ArrayList.. The problem is that I want to have a if statement or something like, that controls if the file
context.openFileOutput("metars_array", Context.MODE_WORLD_READABLE) exists.
I made
if(getApplicationContext().getDir("metars-array", MODE_WORLD_READABLE).exists() || getBaseContext().getDir("metars-array", MODE_WORLD_READABLE).exists()){
ArrayList<Metar> mets = getArrayMetars(getBaseContext());
isMetarsInDb=true;
}
else
isMetarsInDb=false;
The error is that it always entries on the first if, and when I do
ArrayList mets = getArrayMetars(getBaseContext());
it returns an error saying
file "/data/data/android.altimeter.com/app_metars_array" (File Not Found)
I want to do this because if the file not exists (User is using the app for the first time and he never serialized the arraylist) I have to use my method read from XML and then serialize to metars_array file.