Java.io.NotSerializableException

Source: Internet
Author: User
Tags object serialization serialization macbook

The implementation of the Java serialization is simple and requires the serialization of the class to implement the Serializable interface;

You can then use the Objectoutstream write () method output;

Deserialization: Can be entered using the Read () method of ObjectInputStream;

 Serialization and deserialization implementations are simple, but be aware that the class that implements the serializable interface cannot have instantiated objects of other classes;

That is, if the class also contains instances of other classes, then the called class also needs to implement the serializable interface.

Here's a list of the problems I'm having when I'm learning to serialize:

It is troublesome to instantiate the object of the scanner class in the class of the serializable interface, resulting in a running error;

Job Requirements:

Applies object serialization and object deserialization to the file to write to the object, and reads the object out of the input to the console.

:

Task Requirements:

1. Create the product class and declare its properties: Id,name,categories,price
2, inherit the Serializable interface;
3, define the product class construction method;
4. In the test class, create the object of the product class: Iphone,ipad,macbook,iwatch
5. Instantiate object input stream and object output stream;
6. Write four objects of the product class;
7. Read four objects of four product classes from a file.

My Code:

1 Importjava.io.Serializable;2 ImportJava.util.Scanner;3 4  Public classAppleImplementsSerializable {5     PrivateString ID;6     PrivateString name;7     PrivateString categories;8     Private DoublePrice ;9    Scanner sc = new Scanner (system.in);Ten      PublicApple (string ID, string name, string categories,DoublePrice ) { One          This. SetId (ID); A          This. SetName (name); -          This. Setcategories (categories); -          This. Setprice (price); the     } -      PublicString getId () { -         returnID; -     } +      Public voidsetId (String id) { -          This. ID =ID; +     } A      PublicString GetName () { at         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; -     } -      PublicString getcategories () { in         returncategories; -     } to      Public voidsetcategories (String categories) { +          This. Categories =categories; -     } the      Public DoubleGetPrice () { *         returnPrice ; $     }Panax Notoginseng      Public voidSetprice (DoublePrice ) { -          This. Price =Price ; the     } + @Override A      PublicString toString () { the         return"Product ID:" + id + "\ n Product Name:" + name + "\ n Product attributes:" + categories + "\ n Product Price:" + price+ "\ n"; +     } -      $}

 PackageCom.task8;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter; Public classFileTest4 { Public Static voidMain (string[] args) {Try{FileOutputStream fos=NewFileOutputStream ("Apple.txt"); FileInputStream FIS=NewFileInputStream ("Apple.txt"); ObjectOutputStream Obs=NewObjectOutputStream (FOS); ObjectInputStream Ois=NewObjectInputStream (FIS); Obs.writeobject (NewApple ("123", "iphone", "Telephone", 4888.0)); Obs.writeobject (NewApple ("234", "ipad", "Computer", 5088.0)); Obs.writeobject (NewApple ("345", "MacBook", "Computer", 10688.0)); Obs.writeobject (NewApple ("iwatch", "watch", 4799.0)); System.out.println ("Apple Series Product Information:");  for(inti = 0;i<4;i++) {                Try{System.out.println (Ois.readobject ()); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace ();            }} fos.close ();            Fis.close ();            Obs.close ();        Ois.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Operation Result:

The important thing to say three times:

The class that implements the serializable interface cannot have instantiated objects of other classes;

The class that implements the serializable interface cannot have instantiated objects of other classes;

The class that implements the serializable interface cannot have instantiated objects of other classes;

That is, if the class also contains instances of other classes, then the called class also needs to implement the serializable interface.

Java.io.NotSerializableException

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.