Java.io.ObjectOutputStream.writeObject () Method instance

Source: Internet
Author: User

Tag: AMD object SSE string ble tin leo Public input

The java.io.ObjectOutputStream.writeObject (object obj) method writes the specified object to ObjectOutputStream. The class of the object, the signature of the class, and the values of non-transient and non-static fields of the class and all its super-types are written. The default serialized class can be overridden by using the WriteObject and ReadObject methods. Objects referenced by this object are written in the object and the object is fully equivalent to a graph that can be reconstructed by ObjectInputStream.

Statement

The following is a declaration of the Java.io.ObjectOutputStream.writeObject () method

Publicfinalvoid writeobject(Object obj)      
Parameters
    • Obj-the object that will be written

return value

This method does not return a value.

Abnormal
    • Invalidclassexception-The use of serialization for some of the errors of the class.

    • Notserializableexception--Some objects are serialized and do not implement the Java.io.Serializable interface.

    • IOException--Any exception is thrown by the underlying outputstream.

Example

The following example shows the use of the Java.io.ObjectOutputStream.writeObject () method.

PackageCom.Yiibai;ImportJava.Io.*;Public Class Objectoutputstreamdemo { Public Static voidMain(String[]Args) { StringS= "Hello world!"; IntI= 897648764; Try { Create a new file with an ObjectOutputStream FileOutputStream Out = New FileOutputStream("Test.txt"); ObjectOutputStreamOout= New ObjectOutputStream(Out); Write something in the fileOout.WriteObject(S);Oout.WriteObject(I); Close the streamOout.Close(); Create an ObjectInputStream for the file we created before ObjectInputStreamOis= New ObjectInputStream(New FileInputStream("Test.txt")); Read and print what we wrote before System.Out.println("" + (String)Ois.ReadObject()); System.Out.println("" +Ois.ReadObject()); } Catch (Exception Ex)  { Ex. ();  } }}

Let's compile and run the above program, which will produce the following results:

Hello World!

897648764

Java.io.ObjectOutputStream.writeObject () Method instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.