开发者

Can't write an Object. Read-only file system

开发者 https://www.devze.com 2023-03-25 08:22 出处:网络
I\'m trying to save this Object, Inventory, to the internal storage. I have the saving and getting methods in the class itself. When I try and call the save method, I end up with the exception. I had

I'm trying to save this Object, Inventory, to the internal storage. I have the saving and getting methods in the class itself. When I try and call the save method, I end up with the exception. I had the Exception message write to the Logcat, and here's what I开发者_运维问答 got:

08-04 02:32:23.690: VERBOSE/alex(278): /test (Read-only file system)

The file /test is "Read-only file system", but I had allowed writing external storage in the Manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Here's the Inventory class. The last two methods are the save and read methods.

    package com.androidbook.inventoryproject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import android.util.Log;

public class Inventory implements Serializable {
    private static final long serialVersionUID = 1L;
    int numIngred;;
    Ingredient[] ingredients;
    ArrayList ingred = new ArrayList<Ingredient>();

    public Inventory() {
        numIngred = 0;
        ingredients = new Ingredient[numIngred]; 
    }

    public int getNumIngred() {

        return numIngred;
    }

    public String getIngredientName(int n) {
        return ((Ingredient)ingred.get(n)).getName();

    }

    public Ingredient[] getIngredients() {
        return ingredients;
    }

    public Ingredient getIngredient(int n) {
        return (Ingredient)ingred.get(n);
    }

    public void addIngredient(String iname) {
        numIngred++;
        ingred.add(new Ingredient(iname));
    }

    public boolean saveInventory( Inventory inv) {
        File suspend_f = new File("test");
        FileOutputStream   fos  = null;
        ObjectOutputStream oos  = null;
        boolean            keep = true;
        try {
            fos = new FileOutputStream(suspend_f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(inv);
        }
        catch (Exception e) {
            keep = false;
            Log.v("alex", "" + e.getMessage());

        }
        finally {
            try {
                if (oos != null)   oos.close();
                if (fos != null)   fos.close();
                if (keep == false) suspend_f.delete();
            }
            catch (Exception e) { /* do nothing */ }
        }
        return keep;
    }

    public Inventory getInventory() {
        File suspend_f = new File("test");
        Inventory inven = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try{
            fis = new FileInputStream(suspend_f);
            ois = new ObjectInputStream(fis);
            inven = (Inventory)ois.readObject();
        }
        catch (Exception e) {
            String mess = e.getMessage();

        }
        finally {
            try {
                if (fis != null)  
                    fis.close();
                if (ois != null)  
                    ois.close();
            }
            catch (Exception e) { }
        }
        return inven;
    }
}


WRITE_EXTERNAL_STORAGE lets you write to the SD card, not to the filesystem root. You should try this:

File suspend_f = new File(Environment.getExternalStorageDirectory(), "test");

This verifies that the file you are using goes into a writable external folder.

EDIT: there is a bunch of other work you should do to verify that the SD card is available and writable. Read the specs to see how to make your file access robust by checking availability.

0

精彩评论

暂无评论...
验证码 换一张
取 消