jtk42
jtk42

Reputation: 23

Android: cannot write object to file

I'm working on an android project and I have two objects that I will need to save and read. I've written out the code for one of the objects so far but when I run it I get the error "java.io.FileNotFoundException: tripfile.ser: open failed: ENOENT (No such file or directory)"

I had found this post which had a similar problem, but the solution there doesn't seem to work for me. My code is below.

public class FileHandler {
    static Context context;
    static String tripFileName = "tripfile.ser";
    static String oauthFileName = "oauthFile.ser";

    public FileHandler(Context c){
        context=c;
        File dir = new File(context.getFilesDir() + "C:/Users/M/workspace/NerdRanch/FitTravel");
        dir.mkdirs();
        File tripFile = new File(dir, tripFileName);
        File oauthFile = new File(dir, oauthFileName);
    }

   public static void writeTrip(Trip curTrip){
       Trip trip = curTrip;

       FileOutputStream outputStream;

       try {
           outputStream = context.openFileOutput(tripFileName, 0);
           ObjectOutputStream objectStream = new ObjectOutputStream(outputStream);
           objectStream.writeObject(trip);
           objectStream.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   public static Trip readTrip(){
       Trip t;

       try{
           FileInputStream inputStream = new FileInputStream(tripFileName);
           ObjectInputStream objectIn = new ObjectInputStream(inputStream);
           t = (Trip) objectIn.readObject();
           objectIn.close();

           return t;
       }
       catch(Exception e){
           e.printStackTrace();
           return null;
       }
   }

}

Can anyone tell me what I'm doing wrong or point me towards a helpful related post?

Upvotes: 0

Views: 92

Answers (2)

greenapps
greenapps

Reputation: 11214

 "C:/Users/M/workspace/NerdRanch/FitTravel"

That is a file or directory on a windows machine. If you are running around in the bush with your phone you think it can be found? Moreover C: does not exists on an Android/linux file system. And the : character is forbidden.

Upvotes: 0

kamokaze
kamokaze

Reputation: 7370

you try this method to write & read objects to your shared prefs file -it uses google's gson to serialize deserialize your object and write it with the shared pref

... its easy

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; 
import com.google.gson.Gson;

to write the object:

SharedPreferences  mPrefs;
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(object);
        prefsEditor.putString("MyObject", json);
        prefsEditor.commit();

to read the object:

Gson gson = new Gson();
    String json = mPrefs.getString("MyObject", "");
    MyObject object = gson.fromJson(json, MyObject.class);

Upvotes: 1

Related Questions