读取序列化的数组数组

时间:2013-12-13 19:23:16

标签: java arrays serialization

我保存的文件包含序列化的String数组,其中包含3个数组(1 String和2 Double)。

我如何阅读它并重做3个阵列?

以下是我的写作方式:

String[] storeAllArrays[] = {prod, cant, pret};

    ObjectOutputStream out;
    try {
        out = new ObjectOutputStream(new FileOutputStream("test.ser"));
        out.writeObject(storeAllArrays);
        out.flush();
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
    }
编辑:这是我试过的:

 try {
         ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));
    String[] arrayT = (String[]) in.readObject();
        in.close();
         JOptionPane.showMessageDialog(this, ArrayT);
    } catch (IOException ex) {
        Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
    }

3 个答案:

答案 0 :(得分:1)

我会写String和两个双打,作为一个字符串和两个双打。

DataOutputStream dos = new DataOutputStream(new FileOutputStream("test.data"));
dos.writeUTF(prod);
dos.writeDouble(cant);
dos.writeDouble(pret);
dos.close();

阅读

DataInputStream dis = new DataInputStream(new FileInputStream("test.data"));
prod = dis.readUTF();
cant = dis.readDouble();
pret = dis.readDouble();
dis.close();

如果必须使用ObjectOutputStream,则可以使用ObjectInputStream

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.data"));
String[] array = (String[]) dis.readObject();
dis.close();

答案 1 :(得分:1)

序列化和反序列化的简单示例

    String[] prod = { "pr", "od" };
    String[] cant = { "10.0", "20.0" };
    String[] pret = { "30.0", "40.0" };
    String[] storeAllArrays[] = {prod, cant, pret};

    Logger logger = Logger.getLogger(SerializerSample.class.getName());
    String serializedPath = "/tmp/store_test.ser";

    ObjectOutputStream out;
    try {
        out = new ObjectOutputStream(new FileOutputStream(serializedPath));
        out.writeObject(storeAllArrays);
        out.flush();
        out.close();
    } catch (FileNotFoundException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    try {
        InputStream file = new FileInputStream(serializedPath);
        InputStream buffer = new BufferedInputStream(file);
        ObjectInput input = new ObjectInputStream(buffer);

        String[] storeAllArraysREAD[] = (String[][]) input.readObject();
        logger.log(Level.INFO, storeAllArraysREAD.toString());

    } catch (ClassNotFoundException ex) {
        logger.log(Level.SEVERE, "Cannot perform input. Class not found.",
                ex);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Cannot perform input.", ex);
    }

答案 2 :(得分:0)

你听说过ObjectInputStream吗?它用于读取使用ObjectOutputStream创建的文件。你构建一个这样的:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser"));

然后你可以像这样读取数组:

Object[] array = (Object[]) ois.readObject();