{"id":89825,"date":"2025-01-06T19:00:43","date_gmt":"2025-01-06T13:30:43","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89825"},"modified":"2025-01-06T19:39:20","modified_gmt":"2025-01-06T14:09:20","slug":"java-datainputstream-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/","title":{"rendered":"Java DataInputStream Class"},"content":{"rendered":"<p>To read raw data bytes from images, audio, video, etc., utilise an input stream. The FileReader class allows for reading character streams, which can also be read using character-stream data.<\/p>\n<h2>Explanation<\/h2>\n<p>An application can read raw data from the input stream in a machine-independent manner thanks to the Java DataInputStream class.<\/p>\n<p>Typically, a Java application writes data to the data output stream so that a data input stream can read it later.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">InputStream in =newDataInputStream(InputStream in);\r\n<\/pre>\n<h3>Constructor<\/h3>\n<p>DataInputStream(InputStream in) uses the underlying InputStream to create a DataInputStream.<\/p>\n<h3>Method<\/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;\">read(byte[] b)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reads a predetermined number of bytes from the contained input stream and stores them in buffer array b.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">read(byte[] b, int off, int len)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reads data from the contained input stream up to length bytes and stores it in an array.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readBoolean()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reads a single input byte, returns false if the byte is zero, and true otherwise.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readChar()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns a char value after reading two input bytes.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readByte()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns a byte value after reading one input byte.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readFloat()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns a float value after reading four input bytes.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readFully()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Read the number of bytes in the byte array.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readDouble()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns a double value after reading eight bytes of input.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readInt()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns an int value after reading four input bytes.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readLine()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reading a text line<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readLong()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returning a long value after reading eight input bytes<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readShort()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Return a short value after reading two input bytes.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readUnsignedByte()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Read a byte, then give back an integer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">readUnsignedShort()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">After reading two input bytes, an integer array is returned.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">skipBytes()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Skips n bytes of the input stream&#8217;s data.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Sno<\/strong><\/td>\n<td><strong>\u00a0 \u00a0 Method\u00a0<\/strong><\/td>\n<td><strong> \u00a0 Description\u00a0<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td><span style=\"font-weight: 400;\">public final int read(byte[] r, int off, int len)throws IOException<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reads data from the input stream up to len bytes into an array of bytes. gives back the total number of bytes that were read into the buffer; if the file is at the end, it returns -1.<\/span><\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td><span style=\"font-weight: 400;\">Public final int read(byte [] b)throws IOException<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reads a small number of bytes from the inputstream and puts them in the byte array. returns -1 if the file is at the end, otherwise returns the total number of bytes read into the buffer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> public final Boolean readBooolean()throws IOException<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">public final byte readByte()throws IOException<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0public final short readShort()throws IOException<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">public final Int readInt()throws IOException<\/span><\/li>\n<\/ul>\n<\/td>\n<td><span style=\"font-weight: 400;\">These methods will read the bytes from the contained InputStream. gives back the next two InputStream bytes as the designated primitive type.<\/span><\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td><span style=\"font-weight: 400;\">public String readLine() throws IOException<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Opens the input stream and begins reading the next line of text. The characters read are returned as a String after it reads consecutive bytes, translating each byte into a character one at a time until it comes across a line terminator or file end.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\npublic class TechVidvan {\r\n   public static void main(String args[])throws IOException \r\n{\r\n\r\n      DataOutputStream dataOut = new DataOutputStream(new FileOutputStream(\"E:\\\\file.txt\"));\r\n      dataOut.writeUTF(\"welcome to TechVidvan tutorials\");\r\n\r\n      DataInputStream dataIn = new DataInputStream(new FileInputStream(\"E:\\\\file.txt\"));\r\n\r\n      while(dataIn.available()&gt;0) {\r\n         String k = dataIn.readUTF();\r\n         System.out.print(k+\" \");\r\n      }\r\n   }\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\nwelcome to TechVidvan tutorials<\/p>\n<h4>Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;    \r\npublic class TechVidvan\r\n{  \r\n  public static void main(String[] args) throws IOException \r\n{  \r\n    InputStream input = new FileInputStream(\"D:\\\\testout.txt\");  \r\n    DataInputStream inst = new DataInputStream(input);  \r\n    int count = input.available();  \r\n    byte[] ary = new byte[count];  \r\n    inst.read(ary);  \r\n    for (byte bt : ary) \r\n{  \r\n      char k = (char) bt;  \r\n      System.out.print(k+\"-\");  \r\n    }  \r\n  }  \r\n  }<\/pre>\n<p><strong>Output<\/strong><br \/>\nJ-A-V-A<\/p>\n<h4>Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.FileInputStream;\r\nimport java.io.InputStream;\r\nclass Main\r\n {\r\n  public static void main(String args[])\r\n {\r\n\r\n    byte[] array = new byte[100];\r\n\r\n    Try\r\n {\r\n      InputStream input = new FileInputStream(\"input.txt\");\r\n\r\n      System.out.println(\"Bytes in file: \" + input.available());\r\n      input.read(array);\r\n      System.out.println(\"Data read : \");\r\n\r\n\r\n      String data = new String(array);\r\n      System.out.println(data);\r\n\r\n      input.close();\r\n    } \r\ncatch (Exception e)\r\n {\r\n      e.getStackTrace();\r\n    }\r\n  }\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\n<strong>Bytes in file:<\/strong> 16<br \/>\n<strong>Data read:<\/strong><br \/>\nJava programming<\/p>\n<h3>Conclusion<\/h3>\n<p>In Java, DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument. It is capable of reading all Java primitive data types.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To read raw data bytes from images, audio, video, etc., utilise an input stream. The FileReader class allows for reading character streams, which can also be read using character-stream data. Explanation An application can&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447258,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5532,321,296,322,323,5531,263,327,250],"class_list":["post-89825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-datainputstream-class","tag-datainputstream-class-in-java","tag-java","tag-java-datainputstream","tag-java-datainputstream-class","tag-java-datainputstream-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 DataInputStream Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument.\" \/>\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-datainputstream-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java DataInputStream Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-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-06T13:30:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T14:09:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.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 DataInputStream Class - TechVidvan","description":"Java DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument.","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-datainputstream-class\/","og_locale":"en_US","og_type":"article","og_title":"Java DataInputStream Class - TechVidvan","og_description":"Java DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-01-06T13:30:43+00:00","article_modified_time":"2025-01-06T14:09:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.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-datainputstream-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java DataInputStream Class","datePublished":"2025-01-06T13:30:43+00:00","dateModified":"2025-01-06T14:09:20+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/"},"wordCount":514,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.webp","keywords":["datainputstream class","datainputstream class in java","java","java datainputstream","java datainputstream class","java datainputstream 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-datainputstream-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/","name":"Java DataInputStream Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.webp","datePublished":"2025-01-06T13:30:43+00:00","dateModified":"2025-01-06T14:09:20+00:00","description":"Java DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/07\/java-datainputstream.webp","width":1200,"height":628,"caption":"java datainputstream"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-datainputstream-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java DataInputStream 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\/89825","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=89825"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89825\/revisions"}],"predecessor-version":[{"id":447778,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89825\/revisions\/447778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447258"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}