问题描述
我一直在寻找周围很多,无法做到这一点。我创建了一个类的VectorEx,延长矢量,存储两种方法,一种用于保存矢量,一个用于加载它。我知道我必须使用的FileOutputStream和openFileInput(),但我无法创建文件。我仍然在pretty新到Android程序,因此一个简单的解释就是AP preciated。
I've been searching around a lot and couldn't accomplish this. I have created a class 'VectorEx', extending 'Vector', to store two methods, one for saving the vector and one for loading it. I know i have to use FileOutputStream and openFileInput() but I'm unable to create the file. I'm still pretty new to android programming, so a simple explanation is appreciated.
public class VectorEx extends Vector<Subject> {
public void save(String name, Context ctx) {
try {
File file = new File(name);
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = ctx.openFileOutput(name, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void load(String name, Context ctx) {
try {
FileInputStream fis = ctx.openFileInput(name);
ObjectInputStream ois = new ObjectInputStream(fis);
ois.readObject();
}
catch(IOException e) {
e.printStackTrace();
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
这是code我上次使用试过。无论从任何我能理解LogCat中的,似乎这是一个只读文件系统。
This is the code I last tried using. From whatever I could understand of LogCat, it seems that this is a "Read-only file system".
推荐答案
这些权限也许之一是缺少在AndroidManifest:
Maybe one of these permission is missing in your AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
这篇关于如何保存在android的矢量对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!