Question

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.

Was it helpful?

Solution 2

Its Solved:

Check that timerTask:

    t.schedule(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                   FileInputStream fis = getApplicationContext().openFileInput("metars_array");
                   ObjectInputStream is = new ObjectInputStream(fis);
                   Object readObject = is.readObject();
                   is.close();

                    if(readObject != null && readObject instanceof ArrayList) {

                        isInDb = true;
                        metars=(ArrayList<Metar>) readObject;
                        isMetToArrayFin=true;
                        t.cancel();
                    }

                    else{
                        isInDb = false;
                        xmlToStruct();
                        saveMetars(getApplicationContext(), metars);
                        t.cancel();
                    }
            }
             catch (Exception e) {
                        // TODO: handle exceptionrunOnUiThread(new Runnable()
                 isInDb = false;
                 xmlToStruct();
                 saveMetars(getApplicationContext(), metars);
                 t.cancel();
             }

        }
    },0,12000);

OTHER TIPS

From a design perspective, a recommendation would be to store data as data (in XML or a database or some structured format). However, in the interest in answering the question specifically...

"metars-array" does not match the directory "app_metars_array" (not same URI)... Secondly, references to directories and files seem to be mixed. Double-check that the reference is to a file versus a directory (I did not see directory traversal through files but a file reference even though a directory is checked)...

Hence file-not-found error. Pretty straight-forward. Also be sure to use a canonical name (complete path) if the relative path may change. That is, "/data" implies that "/data" is at the root but was it intended as relative?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top