0

我一直在寻找很多,但无法做到这一点。我创建了一个类“VectorEx”,扩展了“Vector”,用于存储两种方法,一种用于保存向量,另一种用于加载它。我知道我必须使用 FileOutputStream 和 openFileInput() 但我无法创建文件。我对 android 编程还是很陌生,所以很感谢一个简单的解释。

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

这是我上次尝试使用的代码。从我对 LogCat 的理解来看,这似乎是一个“只读文件系统”。

4

3 回答 3

1

您的 AndroidManifest 中可能缺少这些权限之一:

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

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
于 2012-06-25T12:16:50.110 回答
0
here are the vector constucts:
Vector( ) 
Vector(int size) 
Vector(int size, int incr) 
Vector(Collection c)

//Demonstrate various Vector operations. 
import java.util.*; 
class VectorDemo { 
    public static void main(String args[]) { 
        // initial size is 3, increment is 2 
        Vector v = new Vector(3, 2); 
        System.out.println("Initial size: " + v.size()); 
        System.out.println("Initial capacity: " + 
        v.capacity()); 

        v.addElement(new Integer(1)); 
        v.addElement(new Integer(2)); 
        v.addElement(new Integer(3)); 
        v.addElement(new Integer(4)); 
        System.out.println("Capacity after four additions: " + 

        v.capacity()); 
        v.addElement(new Double(5.45)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Double(6.08)); 
        v.addElement(new Integer(7)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Float(9.4)); 
        v.addElement(new Integer(10)); 
        System.out.println("Current capacity: " + 
        v.capacity()); 

        v.addElement(new Integer(11)); 
        v.addElement(new Integer(12)); 
        System.out.println("First element: " + 
        (Integer)v.firstElement()); 
        System.out.println("Last element: " + 
        (Integer)v.lastElement()); 

        if(v.contains(new Integer(3))) 
            System.out.println("Vector contains 3."); 
            // enumerate the elements in the vector. 
            Enumeration vEnum = v.elements(); 
            System.out.println("\\nElements in vector:"); 
            while(vEnum.hasMoreElements()) 
                System.out.print(vEnum.nextElement() + " "); 
                System.out.println(); 
            } 
        }
}



The output from this program is shown here:

Initial size: 0 
Initial capacity: 3 
Capacity after four additions: 5 
Current capacity: 5 
Current capacity: 7 
Current capacity: 9 
First element: 1 
Last element: 12 
Vector contains 3. 
Elements in vector: 
1 2 3 4 5.45 6.08 7 9.4 10 11 12
于 2012-06-25T12:27:02.463 回答
0

我很确定您正在尝试写入根目录。您可能希望写入应用程序的目录或外部存储(如果有),而不是根目录。

于 2012-06-25T12:38:21.787 回答