【问题标题】:Getting OptionalDataException because of primitive int value , but how to avoid it in JAVA由于原始 int 值而获取 OptionalDataException,但如何在 JAVA 中避免它
【发布时间】:2012-05-09 09:09:09
【问题描述】:

我的代码-

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 在 java.io.ObjectInputStream.readObject0(未知来源)在 java.io.ObjectInputStream.readObject(Unknown Source) 在 com.practise.interview.nio.ObjectStreamExample.main(ObjectStreamExample.java:62)

之所以提出这个问题,是因为我认为 -

尝试读取对象时, 流是原始数据。在这种情况下,OptionalDataException 的 长度字段设置为原始数据的字节数 立即从流中读取,并且 eof 字段设置为 假的。

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

【问题讨论】:

    标签: java serialization io datainputstream


    【解决方案1】:

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

    写出person对象本身,如果你想在之后读取Person

    stream.writeObject(person);
    

    附录:如果编写 String 的行为与任何其他 Object 一样,则您将获得 ClassCastException,因为无法将 String 强制转换为 Person .无论如何,你写的和你读的不匹配导致了你得到的错误。

    【讨论】:

    【解决方案2】:

    根据规范

    异常指示由于未读取的原始数据或流中属于序列化对象的数据结束而导致对象读取操作失败。有两种情况可能会抛出这个异常:

    • 尝试读取对象中的下一个元素时 流是原始数据。在这种情况下,OptionalDataException 的 长度字段设置为原始数据的字节数 立即从流中读取,并且 eof 字段设置为 假的。
    • 试图读取数据的末尾 类定义的 readObject 或 readExternal 方法。在这种情况下, OptionalDataException 的 eof 字段设置为 true,并且长度 字段设置为 0。

      stream.writeUTF(person.toString()); // 问题来了

    在这里,您以 UTF 形式编写,而在另一端,您以 Person 对象形式阅读。 你应该改变它-

    stream.writeObject(person);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2011-02-12
      • 1970-01-01
      • 2017-01-23
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多