{"id":89821,"date":"2025-01-13T18:00:37","date_gmt":"2025-01-13T12:30:37","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89821"},"modified":"2025-01-13T18:40:22","modified_gmt":"2025-01-13T13:10:22","slug":"java-dataoutputstream-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/","title":{"rendered":"Java DataOutputStream Class"},"content":{"rendered":"<p>Java is a wide range and used efficiently for many purposes. DataOutputStream Classes are used in various applications of Java. It shows various methods for the data output of Stream.<\/p>\n<h2>Explanation<\/h2>\n<ul>\n<li>The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.<\/li>\n<li>Data that a data input stream can subsequently read is often written by Java applications using the data output stream.<\/li>\n<\/ul>\n<h3>Declaration<\/h3>\n<p>public class DataOutputStream extends<br \/>\nFilterOutputStream implements DataOutput<\/p>\n<h3>Class Methods:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Method\u00a0<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int size()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It returns the number of bytes written to the data output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void write(int b)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to write the specified byte to the underlying output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void write(byte[] b, int off, int len)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It can be used to write Len bytes of data to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeBoolean(boolean v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It writes Boolean values to the output stream as 1-byte values.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeChar(int v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to write a 2-byte value representing char to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeChars(String s)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It writes a string as a series of characters to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeByte(int v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to write a single byte as a value of one byte to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeBytes(String s)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to write a string as a series of bytes to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeInt(int v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">An int is written to the output stream using it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeShort(int v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed for writing briefs to the output stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void writeLong(long v)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Writing a long to the output stream is done with it.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Fields:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Protected int<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Written<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The total number of bytes written to the output stream thus far.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Example:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.java;  \r\nimport java.io.*;  \r\npublic class TechVidvan{  \r\n    public static void main(String[] args)  {  \r\n        FileOutputStream file = new FileOutputStream(D:\\\\testout.txt);  \r\n        DataOutputStream data = new DataOutputStream(file);  \r\n        data.writeInt(65);  \r\n        data.flush();  \r\n        data.close();  \r\n        System.out.println(\"Java Learner in TechVidvan\");  \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nJava Learner in TechVidvan<\/p>\n<h4>Example 2<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*; \r\n\r\nclass TechVidvan{ \r\n                public static void main(String args[]) throws IOException { \r\n                try ( DataOutputStream dout = new DataOutputStream(new FileOutputStream(\"file.dat\")) ) { \r\n                        dout.writeDouble(1.1); \r\n                        dout.writeInt(55); \r\n                        dout.writeBoolean(true); \r\n                        dout.writeChar('4'); \r\n                } \r\n  \r\n                catch (FileNotFoundException ex) { \r\n                        System.out.println(\"File not found\"); \r\n                        return; \r\n                } \r\n  \r\n                try ( DataInputStream din =new DataInputStream(new FileInputStream(\"file.dat\")) ) { \r\n                        double a = din.readDouble(); \r\n                        int b = din.readInt(); \r\n                        boolean c = din.readBoolean(); \r\n                        char d = din.readChar(); \r\n                        System.out.println(\"Values: \" + a + \" \" + b + \" \" + c + \" \" + d); \r\n                } \r\n  \r\n                                catch (FileNotFoundException e) { \r\n                        System.out.println(\"file not found\"); \r\n                        return; \r\n                } \r\n        } \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nValues: 1.1 55 true 4<\/p>\n<h4>Class Constructors:<\/h4>\n<p>DataOutputStream with out<br \/>\noutput stream of out<\/p>\n<h3>DataOutputStream(OutputStream out)<\/h3>\n<ul>\n<li>By doing this, a new data output stream is created and data is written to the designated underlying output stream.<\/li>\n<li>A new data output stream is created to write data to the designated underlying output stream. The printed counter has a zero value.<\/li>\n<\/ul>\n<p><strong>Parameters: out:<\/strong> the output stream being used, which can be saved for later use.<\/p>\n<h4>Fields:<\/h4>\n<ul>\n<li><strong>protected int written: <\/strong>This is the total amount of bytes written to the data output stream thus far.<\/li>\n<li><strong>protected OutputStream out:<\/strong> This is the stream of output that has to be filtered.<\/li>\n<\/ul>\n<h4>Uses:<\/h4>\n<ul>\n<li>It is common practice to combine DataOutputStream and DataInputStream.<\/li>\n<li>The underlying stream indicated by out is automatically closed when a DataOutputStream is closed (by calling close()).<\/li>\n<li>The close() method is no longer explicitly called. The try-with-resources handle that construct.<\/li>\n<\/ul>\n<h3>Importance of DataOutputStream:<\/h3>\n<ul>\n<li>It is common practice to combine DataOutputStream and DataInputStream.<\/li>\n<li>The underlying stream indicated by out is automatically closed when a DataOutputStream is closed (by calling close()).<\/li>\n<li>The close() method is no longer explicitly called. The try-with-resources handle that construct.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>The DataOutputStream is used in various areas in Java. Many examples are used for data output in a detailed manner.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a wide range and used efficiently for many purposes. DataOutputStream Classes are used in various applications of Java. It shows various methods for the data output of Stream. Explanation The Java DataOutputStream&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5529,318,296,319,320,5530,263,327,250],"class_list":["post-89821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-dataoutputstream-class","tag-dataoutputstream-class-in-java","tag-java","tag-java-dataoutputstream","tag-java-dataoutputstream-class","tag-java-dataoutputstream-class-with-example","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-learn-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java DataOutputStream Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.\" \/>\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-dataoutputstream-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java DataOutputStream Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-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=\"2025-01-13T12:30:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-13T13:10:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java DataOutputStream Class - TechVidvan","description":"The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.","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-dataoutputstream-class\/","og_locale":"en_US","og_type":"article","og_title":"Java DataOutputStream Class - TechVidvan","og_description":"The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-01-13T12:30:37+00:00","article_modified_time":"2025-01-13T13:10:22+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java DataOutputStream Class","datePublished":"2025-01-13T12:30:37+00:00","dateModified":"2025-01-13T13:10:22+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/"},"wordCount":505,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.webp","keywords":["dataoutputstream class","dataoutputstream class in java","java","java dataoutputstream","java dataoutputstream class","java dataoutputstream class with example","java tutorial for beginners","java tutorials","Learn Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/","name":"Java DataOutputStream Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.webp","datePublished":"2025-01-13T12:30:37+00:00","dateModified":"2025-01-13T13:10:22+00:00","description":"The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-dataoutputstream.webp","width":1200,"height":628,"caption":"java dataoutputstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-dataoutputstream-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java DataOutputStream Class"}]},{"@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\/89821","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=89821"}],"version-history":[{"count":6,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89821\/revisions"}],"predecessor-version":[{"id":447781,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89821\/revisions\/447781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447253"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}