Objectinputstream and obejctoutputstream

Source: Internet
Author: User

Objectinputstream and obejctoutputstream support serialization of basic data and objects.

Objectoutputstream provides persistent storage for "Basic Data Objects". objectinputstream provides read for "Basic Data Objects.

Public class objectstreamtest {Private Static final string Path = "d :\\ baiduyun \ filetest \ rrr.txt"; public static void main (string [] ARGs) throws exception {testwrite (); testread ();} public static void testwrite () throws filenotfoundexception, ioexception {objectoutputstream out = new objectoutputstream (New fileoutputstream (new file (PATH ))); out. writeboolean (true); out. writebyte (byte) 65); out. writechar ('A'); out. writeint (0, 20131015); out. writefloat (3.14f); out. writedouble (1.414d); // write the hashmap object hashmap map = new hashmap (); map. put ("one", "Red"); map. put ("two", "green"); map. put ("three", "blue"); out. writeobject (MAP); // write the custom box object. Box implements the serializable Interface Box = new box ("desk", 80, 48); out. writeobject (box); out. close ();} public static void testread () throws filenotfoundexception, ioexception, classnotfoundexception {objectinputstream in = new objectinputstream (New fileinputstream (PATH); system. out. printf ("Boolean: % B \ n", in. readboolean (); system. out. printf ("Byte: % d \ n", (IN. readbyte () & 0xff); system. out. printf ("char: % C \ n", in. readchar (); system. out. printf ("int: % d \ n", in. readint (); system. out. printf ("float: % F \ n", in. readfloat (); system. out. printf ("Double: % F \ n", in. readdouble (); // read the hashmap object hashmap map = (hashmap) in. readobject (); iterator iter = map. entryset (). iterator (); While (ITER. hasnext () {map. entry entry = (map. entry) ITER. next (); system. out. printf ("%-6 s -- % s \ n", entry. getkey (), entry. getvalue ();} // read the box object. Box implements the serializable Interface Box = (box) in. readobject (); system. out. println ("box:" + box); In. close ();} static class box implements serializable {private int width; private int height; private string name; public box (string name, int width, int height) {This. name = Name; this. width = width; this. height = height;} @ override Public String tostring () {return "[" + name + ": (" + width + "," + height + ")] ";}}}

 

Objectinputstream and obejctoutputstream

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.