0

私はたくさん探し回っていましたが、これを達成できませんでした。「Vector」を拡張したクラス「VectorEx」を作成し、2 つのメソッドを格納しました。1 つはベクトルを保存するためのもので、もう 1 つはベクトルをロードするためのものです。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にこれらの権限の1つが欠落している可能性があります。

<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

ルートディレクトリに書き込もうとしているに違いありません。おそらく、ルートではなく、アプリのディレクトリまたは外部ストレージ (存在する場合) に書き込みたいと思うでしょう。

于 2012-06-25T12:38:21.787 に答える
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 に答える