无法写入对象。只读文件系统

时间:2011-08-04 03:23:15

标签: android file object readonly

我正在尝试将此Object,Inventory保存到内部存储。我在课堂上有保存和获取方法。当我尝试调用save方法时,我最终得到了异常。我将异常消息写入Logcat,这是我得到的:

08-04 02:32:23.690:VERBOSE / alex(278):/ test(只读文件系统)

文件/测试是“只读文件系统”,但我允许在Manifest文件中编写外部存储:

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

这是库存类。最后两种方法是保存和读取方法。

    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;
    }
}

1 个答案:

答案 0 :(得分:2)

WRITE_EXTERNAL_STORAGE允许您写入SD卡,而不是文件系统根目录。你应该试试这个:

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

这将验证您使用的文件是否进入可写外部文件夹。

编辑:你应该做一些其他工作来验证SD卡是否可用和可写。阅读specs,了解如何通过检查可用性来确保您的文件访问能力。

相关问题