由于原始int值而获取OptionalDataException,但如何在JAVA中避免它

时间:2012-04-29 07:59:28

标签: java serialization io datainputstream

我的代码 -

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Person person = new Person();
        person.setFirstName("Abhishek");
        person.setLastName("Choudhary");
        person.setAge(25);
        person.setHouseNum(256);
        ObjectOutputStream stream = null;
        try {
            stream = new ObjectOutputStream(new FileOutputStream(new File("Serialize.txt")));
            stream.writeUTF(person.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally{
            if(stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        ObjectInputStream input = null;

        try {
            input = new ObjectInputStream(new FileInputStream(new File("Serialize.txt")));

            Person person2 = (Person) input.readObject();
            System.out.println(person2.getFirstName());
            System.out.println(person2.getLastName());
            System.out.println(person2.getAge());
            System.out.println(person2.getHouseNum());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            if(input != null)
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }


    }

}

和一个Person bean文件。

我正在异常

  

java.io.OptionalDataException at   java.io.ObjectInputStream.readObject0(未知来源)at   java.io.ObjectInputStream.readObject(未知来源)at   com.practise.interview.nio.ObjectStreamExample.main(ObjectStreamExample.java:62)

这是因为我认为 -

  

尝试在下一个元素中读取对象   流是原始数据。在这种情况下,OptionalDataException的   length字段设置为原始数据的字节数   可立即从流中读取,并将eof字段设置为   假的。

但是如何避免它,因为我知道我设置了一个原始值,所以要避免。?

2 个答案:

答案 0 :(得分:6)

您正在撰写String并尝试阅读Person。这不是序列化的工作原理。在序列化的上下文中,UTF字符串被认为是原始数据,因为它不包含对象信息(类名,属性等),而只包含字符串数据。

如果您想在之后阅读person,请写出Person对象本身:

stream.writeObject(person);

附录:如果写String的行为与任何其他Object的行为相同,则会得到ClassCastException,因为String无法投放到Person。在任何情况下,您所写的内容与您阅读的内容之间的不匹配都会导致您所犯的错误。

答案 1 :(得分:1)

符合规范

异常表示由于未读原始数据导致对象读取操作失败,或者属于流中序列化对象的数据结束。在两种情况下可能会抛出此异常:

  • 尝试在下一个元素中读取对象 流是原始数据。在这种情况下,OptionalDataException的 length字段设置为原始数据的字节数 可立即从流中读取,并将eof字段设置为 假的。
  • 尝试通过a读取数据消耗的结尾 类定义的readObject或readExternal方法。在这种情况下, OptionalDataException的eof字段设置为true,长度为 field设置为0。

    stream.writeUTF(person.toString()); //问题在这里

这里你写的是UTF,另一端你正在读作一个Person对象。 你应该改变它 -

stream.writeObject(person);