保存到文件矢量矢量

时间:2021-12-08 16:51:16

i'm writing to you for a problem that i can't solve. I have a Vector of Vector.

我正在给你写一个我无法解决的问题。我有一个矢量矢量。

Vector<Vector<Item>> vectorItem;

I don't know how can i save it into a file, and after, how to load.

我不知道如何将其保存到文件中,之后如何加载。

I try this:

我试试这个:

public void save(String name, Context ctx, Vector<Vector<Item>> vectorItem) {
        try {
            String sdCard = Environment.getExternalStorageDirectory().toString();
            File dir = new File(sdCard + "/dir");
            File file = new File(dir.getAbsolutePath(), name);
            if(!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = ctx.openFileOutput(name, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(vectorItem);
            oos.close();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

    public Vector<Vector<Item>> load(String name, Context ctx) {        
        Vector<Vector<Item>> vectorItem; = null;
        String sdCard = Environment.getExternalStorageDirectory().toString();
        File dir = new File(sdCard + "/dir");
        try {
            FileInputStream fis = ctx.openFileInput(name);
            ObjectInputStream ois = new ObjectInputStream(fis);
            vectorItem = (Vector<Vector<Item>>) ois.readObject();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        return vectorSezioni;
    }

But this is the error: 12-29 16:57:07.140: W/System.err(32681): java.io.IOException: open failed: ENOENT (No such file or directory)

但这是错误:12-29 16:57:07.140:W / System.err(32681):java.io.IOException:open failed:ENOENT(没有这样的文件或目录)

1 个解决方案

#1


2  

First you must guarantee you have WRITE_EXTERNAL_STORAGE permission 'cause you're writing to the SD card.

首先,您必须保证您拥有WRITE_EXTERNAL_STORAGE权限,因为您正在写入SD卡。

Before the call to

在致电之前

File file = new File(dir.getAbsolutePath(), name);

you should call

你应该打电话

dir.mkdirs();

to be sure the directory gets created.

确保目录被创建。

Then, after you write the object to the output stream, you must flush it for all the data to get written before its closed

然后,在将对象写入输出流之后,必须刷新它以便在关闭之前写入所有数据

oos.flush();

Deserialization method should take into consideration the directory an the file name:

反序列化方法应该考虑目录的文件名:

public Vector<Vector<Item>> load(String name, Context ctx) {        
    Vector<Vector<Item>> vectorItem; = null;
    String sdCard = Environment.getExternalStorageDirectory().toString();
    File dir = new File(sdCard + "/dir");
    File file = new File(dir.getAbsolutePath(), name);
    try {
        FileInputStream fis = ctx.openFileInput(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        vectorItem = (Vector<Vector<Item>>) ois.readObject();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
    return vectorSezioni;
}

Hope it helps.

希望能帮助到你。

#1


2  

First you must guarantee you have WRITE_EXTERNAL_STORAGE permission 'cause you're writing to the SD card.

首先,您必须保证您拥有WRITE_EXTERNAL_STORAGE权限,因为您正在写入SD卡。

Before the call to

在致电之前

File file = new File(dir.getAbsolutePath(), name);

you should call

你应该打电话

dir.mkdirs();

to be sure the directory gets created.

确保目录被创建。

Then, after you write the object to the output stream, you must flush it for all the data to get written before its closed

然后,在将对象写入输出流之后,必须刷新它以便在关闭之前写入所有数据

oos.flush();

Deserialization method should take into consideration the directory an the file name:

反序列化方法应该考虑目录的文件名:

public Vector<Vector<Item>> load(String name, Context ctx) {        
    Vector<Vector<Item>> vectorItem; = null;
    String sdCard = Environment.getExternalStorageDirectory().toString();
    File dir = new File(sdCard + "/dir");
    File file = new File(dir.getAbsolutePath(), name);
    try {
        FileInputStream fis = ctx.openFileInput(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        vectorItem = (Vector<Vector<Item>>) ois.readObject();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
    return vectorSezioni;
}

Hope it helps.

希望能帮助到你。