dancingbush
dancingbush

Reputation: 2281

File Not Found Saving Object to Phone File Directory

I am trying to write a object to file and retrieve it later from a different activity. When opening the Input stream to retrieve the object I get the IO exception:

java.io.FileNotFoundException: /data/data/com.mooney.diveapp/files/savedDiveLocations: open failed: ENOENT (No such file or directory)

The String and File initialisation:

String fileName="savedDiveLocations";
File theFileName; 


theFileName= new File(objectConetxActivity.getFilesDir(), fileName);    

The code here returns a object from the file where it is saved previously:

String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
        ObjectInputStream ois = null;
        FileInputStream streamIn=null;
        Object locationObject = null;

        try{

            // except thrown here
            streamIn = new FileInputStream(fileAddress); // address of file
            ois = new ObjectInputStream(streamIn);
            locationObject = ois.readObject();
        }catch (Exception e){

And writing a object to file:

ObjectOutputStream oos = null;
    FileOutputStream fout = null;


    // save to internal durectpry as opposed to exteranl SD card
    try{

        String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
        fout = new FileOutputStream(fileAddress); // filepath
        oos = new ObjectOutputStream(fout);
        oos.writeObject(mapLocationsObject);

    }catch(Exception ex){...

Any input appreciated.

Upvotes: 1

Views: 142

Answers (1)

joao2fast4u
joao2fast4u

Reputation: 6892

Just executing theFileName= new File(objectConetxActivity.getFilesDir(), fileName); alone will not create your file.

You have to call theFileName.createNewFile();. And make sure that method returns true. That means the file was effectively created.

Upvotes: 1

Related Questions