Android Can';不要写对象。只读文件系统
我正在尝试将此对象(库存)保存到内部存储器。我在类本身中有保存和获取方法。当我尝试调用save方法时,最终出现异常。我将异常消息写入Logcat,下面是我得到的: 08-04 02:32:23.690:VERBOSE/alex(278):/test(只读文件系统) 文件/测试是“只读文件系统”,但我允许在清单文件中写入外部存储:Android Can';不要写对象。只读文件系统,android,file,object,readonly,Android,File,Object,Readonly,我正在尝试将此对象(库存)保存到内部存储器。我在类本身中有保存和获取方法。当我尝试调用save方法时,最终出现异常。我将异常消息写入Logcat,下面是我得到的: 08-04 02:32:23.690:VERBOSE/alex(278):/test(只读文件系统) 文件/测试是“只读文件系统”,但我允许在清单文件中写入外部存储: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"><
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
这是库存类。最后两种方法是save和read方法
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;
}
}
package com.androidbook.inventory项目;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.io.Serializable;
导入java.util.ArrayList;
导入android.util.Log;
公共类清单实现了可序列化{
私有静态最终长serialVersionUID=1L;
int numIngred;;
成分[]成分;
ArrayList ingred=新的ArrayList();
公共库存(){
numIngred=0;
配料=新配料[numIngred];
}
public int getNumIngred(){
返回Numingerd;
}
公共字符串getIngredientName(int n){
return((配料)ingred.get(n)).getName();
}
公共成分[]获取成分(){
返回成分;
}
公共成分(国际n){
返回(成分)ingred.get(n);
}
公共无效添加元素(名称中的字符串){
numIngred++;
添加(新成分(iname));
}
公共布尔存储库存(库存库存){
文件挂起=新文件(“测试”);
FileOutputStream=null;
ObjectOutputStream oos=null;
布尔保持=真;
试一试{
fos=新文件输出流(挂起);
oos=新对象输出流(fos);
oos.writeObject(inv);
}
捕获(例外e){
保持=假;
Log.v(“alex”,“e.getMessage());
}
最后{
试一试{
如果(oos!=null)oos.close();
如果(fos!=null)fos.close();
如果(keep==false)挂起;
}
catch(异常e){/*什么也不做*/}
}
返回保持;
}
公共库存getInventory(){
文件挂起=新文件(“测试”);
存货存货存货=空;
FileInputStream fis=null;
ObjectInputStream ois=null;
试一试{
fis=新文件输入流(挂起);
ois=新的ObjectInputStream(fis);
inven=(Inventory)ois.readObject();
}
捕获(例外e){
字符串mess=e.getMessage();
}
最后{
试一试{
如果(fis!=null)
fis.close();
如果(ois!=null)
ois.close();
}
捕获(例外e){}
}
返回inven;
}
}
WRITE\u EXTERNAL\u STORAGE
允许您写入SD卡,而不是文件系统根目录。你应该试试这个:
File suspend_f = new File(Environment.getExternalStorageDirectory(), "test");
这将验证您正在使用的文件是否进入可写的外部文件夹
编辑:为了验证SD卡是否可用和可写,您还需要做很多其他工作。请阅读,了解如何通过检查可用性使文件访问更健壮。哦,等等,我实际上想写入文件系统根目录。我该怎么做?谢谢你不能。您基本上只能向应用程序的专用存储区和外部存储区写入数据。@Chris是对的。也许,如果您在设备上建立根并修改根分区的只读特性,您就可以这样做,但在未修改的Android设备上,您永远无法这样做。你可能想做什么,需要你特别写根?好吧,我真的不知道我在说什么。我不知道根是什么。我只想保存这个对象,然后在应用程序中访问它!谢谢文件系统根目录(如您前面提到的)。如果您只想保存它并在以后访问它,那么就不需要写入文件系统根目录。