{"id":89833,"date":"2024-12-02T18:00:59","date_gmt":"2024-12-02T12:30:59","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89833"},"modified":"2024-12-02T19:09:24","modified_gmt":"2024-12-02T13:39:24","slug":"java-objectstream-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/","title":{"rendered":"Java ObjectStream Class with Examples"},"content":{"rendered":"<p>Object streams, like data streams, allow for the I\/O of primitive data types. Most, but not all, standard classes enable their objects to be serialized. Those that do implement the Serializable marker interface.<\/p>\n<p>ObjectInputStream and ObjectOutputStream are the object stream classes. These classes implement ObjectInput and ObjectOutput, which are DataInput and DataOutput subinterfaces.<\/p>\n<p>That is, all of the primitive data I\/O methods covered in Data Streams are also implemented in object streams. As a result, an object stream can contain both primitive and object values. This is demonstrated by the ObjectStreams example.<\/p>\n<p>With a few exceptions, ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better. Then, a Calendar object is written in the data file.<\/p>\n<h2>Complex Object Output and Input<\/h2>\n<p>The writeObject and readObject methods are simple to use, but they contain complex object management logic. This is unimportant for a class like Calendar, which only holds primitive values.<\/p>\n<p>Many objects, however, contain references to other objects. If readObject is to recreate an object from a stream, it must be able to recreate all the objects to which the original object referred.<\/p>\n<p>These additional objects may have their references, for example. WriteObject traverses the entire web of object references and writes all objects in that web to the stream.<\/p>\n<p>As a result, a single call to writeObject can result in many objects being written to the stream.<\/p>\n<p>What happens if two objects on the same stream both contain references to the same object? Will they both refer to the same object when read back? Yes, the answer is &#8220;yes.&#8221; A stream can only have one copy of an object, but it can have unlimited references to it. As a result, if you explicitly write an object to a stream twice, you&#8217;re only writing the reference twice.<\/p>\n<p><strong>For instance, consider the following code, which writes an object ob twice to a stream:<\/strong><\/p>\n<p class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Object ob = new Object(); out.writeObject(ob); out.writeObject(ob); Because each writeObject must be matched by a readObject, the code that reads the stream back will look like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Object ob1 = in.readObject(); Object ob2 = in.readObject();<\/pre>\n<p>This yields two objects.<\/p>\n<p>ObjectInputStream Operation The ObjectInputStream is primarily used to read data written by the ObjectOutputStream.<\/p>\n<p>Essentially, the ObjectOutputStream converts Java objects into streams. This is referred to as serialization. These converted streams can be saved to files or transmitted over networks.<\/p>\n<p>If we need to read those objects, we&#8217;ll use the ObjectInputStream to convert the streams back to objects. This is referred to as deserialization.<\/p>\n<h3>Construct an ObjectInputStream:<\/h3>\n<p>We must first import the java.io.ObjectInputStream package to create an object input stream. Here&#8217;s how to make an input stream after we&#8217;ve imported the package.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/Creates a file input stream linked with the specified file\r\nFileInputStream fileStream = new FileInputStream(String file);\r\n\r\n\/\/ Creates an object input stream using the file input stream\r\nObjectInputStream objStream = new ObjectInputStream(fileStream);<\/pre>\n<h3>ObjectInputStream methods:<\/h3>\n<p>The ObjectInputStream class implements the InputStream class&#8217;s methods.<\/p>\n<p>Method read()<br \/>\nread() returns a single byte from the input stream.<br \/>\nreadBoolean() is a function that reads boolean data.<br \/>\nreadChar() &#8211; reads data in character format.<br \/>\nreadInt() is a function that reads integer data.<br \/>\nreadObject() is a function that retrieves an object from the input stream.<\/p>\n<h4>Example:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.ObjectStreamClass;\r\nimport java.util.Calendar;\r\npublic class TechVidvan{\r\n    public static void main(String[] args) {\r\n        ObjectStreamClass osc = ObjectStreamClass.lookup(String.class);        System.out.println(\"String in TechVidvan: \" + osc.getField(\"class\"));\r\n        ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);\r\n        System.out.println(\"Calendar in TechVidvan: \" + osc2.getField(\"class\"));\r\n    }\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\n<strong>String in TechVidvan:<\/strong> null<br \/>\n<strong>Calendar in TechVidvan:<\/strong> null<\/p>\n<h3>Complex Object Output and Input<\/h3>\n<p>The writeObject and readObject methods are simple to use, but they contain complex object management logic. This is unimportant for a class like Calendar, which only holds primitive values. Many objects, however, include references to other objects. If readObject is to recreate an object from a stream, it must be able to recreate all the objects to which the original object referred. These additional objects may have their references.<\/p>\n<p>For example. WriteObject traverses the entire web of object references and writes all objects in that web to the stream. As a result, a single call to writeObject can result in many objects being written to the stream. What happens if two objects on the same stream both contain references to the same object? Will they both refer to the same object when read back? Yes, the answer is &#8220;yes.&#8221; A stream can only have one copy of an object, but it can have unlimited references to it. As a result, if you explicitly write an object to a stream twice, you&#8217;re only writing the reference twice.<\/p>\n<p><strong>For instance, consider the following code, which writes an object ob twice to a stream:<\/strong><\/p>\n<p>Object ob = new Object(); out.writeObject(ob); out.writeObject(ob); Because each writeObject must be matched by a readObject, the code that reads the stream back will look like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Object ob1 = in.readObject(); Object ob2 = in.readObject();<\/pre>\n<p>This yields two objects.<\/p>\n<p>ObjectOutputStream named output using the FileOutputStream named file<br \/>\nObjectInputStream named input using the FileInputStream named fileStream<\/p>\n<p><strong>A Dog class object dog<\/strong><\/p>\n<p>We then used the object output stream to write the object to the file. The object input stream is used to read the object from the file.<\/p>\n<p>The Dog class implements the Serializable interface. This is because the ObjectOutputStream only writes serializable objects to the output stream.<\/p>\n<h3>Conclusion<\/h3>\n<p>Java streams support functional-style operations on element streams. A stream is a non-mutable collection of functions applied to data in a specific order. A stream is not a collection of elements that can be stored.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object streams, like data streams, allow for the I\/O of primitive data types. Most, but not all, standard classes enable their objects to be serialized. Those that do implement the Serializable marker interface. ObjectInputStream&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447241,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5521,5685,263,327,250,5523,5522],"class_list":["post-89833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-objectstream-class","tag-java-objectstream-class-with-examples","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-learn-java","tag-objectstream-class","tag-objectstream-class-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java ObjectStream Class with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java ObjectStream Class with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-02T12:30:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-02T13:39:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java ObjectStream Class with Examples - TechVidvan","description":"ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/","og_locale":"en_US","og_type":"article","og_title":"Java ObjectStream Class with Examples - TechVidvan","og_description":"ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-12-02T12:30:59+00:00","article_modified_time":"2024-12-02T13:39:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java ObjectStream Class with Examples","datePublished":"2024-12-02T12:30:59+00:00","dateModified":"2024-12-02T13:39:24+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/"},"wordCount":862,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp","keywords":["java","java objectStream class","java objectstream class with examples","java tutorial for beginners","java tutorials","Learn Java","objectStream class","objectStream class in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/","name":"Java ObjectStream Class with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp","datePublished":"2024-12-02T12:30:59+00:00","dateModified":"2024-12-02T13:39:24+00:00","description":"ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-objectstream.webp","width":1200,"height":628,"caption":"java objectstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-objectstream-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java ObjectStream Class with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89833"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89833\/revisions"}],"predecessor-version":[{"id":447760,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89833\/revisions\/447760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447241"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}