English 中文(简体)
由于原始的隐性价值,可选择任择性数据,但如何避免在JAVA中出现。
原标题:Getting OptionalDataException because of primitive int value , but how to avoid it in JAVA

我的法典

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();
                }
        }


    }

}

一个人是案卷。

我成为例外。

java.io.OptionalDataException at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at com.practise.interview.nio.ObjectStreamExample.main(ObjectStreamExample.java:62)

这是因为我认为——

An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException s length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.

但是,如果我知道,如何避免这种做法,那么,那是避免的。

最佳回答

页: 1 这并不是序列化是如何运作的。 在序列化方面,UTF的扼杀被认为是原始数据,因为其中不包括物体信息(类别名称、属性等),而只包括插图数据。

www.un.org/Depts/DGACM/index_french.htm 如果你希望读到<代码>Person,那么:

stream.writeObject(person);

<><>> 如果书写<代码>String,则与任何其他Object一样,请上ClassCastException,因为String不能上Person/code>。 无论如何,你所写的内容与你所读内容之间的不匹配造成了你所发现的错误。

问题回答

具体化

表示物体由于原始数据无法读取,或者在流中属于序列化物体的数据终结而未能读到运行的情况除外。 这一例外可分为两例:

  • An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException s length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.
  • An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. In this case, the OptionalDataException s eof field is set to true, and the length field is set to 0.

    stream.writeUTF(person.toString()); // Problem is here

Here you are write as UTF and in other end You are reading as a Person Object. You should change it -

stream.writeObject(person);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...