使用try-with-resources重写此代码

问题描述

代码用于通过Java序列化复制实例。它使用传统的try-catch-finally编写方法。可以更改为 try-with-resources 形式吗?(代码中的 DeepConcretePrototype 是普通的Java对象)

    /**
     * Clone an instance through java serialization
     * @return
     */
    public DeepConcretePrototype deepCloneBySerializable() {
        DeepConcretePrototype clone = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            //Output an instance to memory
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
            objectOutputStream.flush();
            //Read instance from memory
            byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            clone = (DeepConcretePrototype)objectInputStream.readobject();
        } catch (IOException e) {
            e.printstacktrace();
        } catch (ClassNotFoundException e) {
            e.printstacktrace();
        } finally {
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
            if (byteArrayInputStream != null) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
        }
        return clone;
    }

解决方法

是的,您可以使用try-with-resources,但这有点棘手,因为read的成功取决于write的成功。编写它的一种方法是使用嵌套的try

public DeepConcretePrototype deepCloneBySerializable() {
    DeepConcretePrototype clone = null;

    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
        //Output an instance to memory
        objectOutputStream.writeObject(this);
        objectOutputStream.flush();

        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
             ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
            //Read instance from memory
            clone = (DeepConcretePrototype) objectInputStream.readObject();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return clone;
}