Java.io.DataInputStream class in Java | Set 1 Last Updated : 11 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report A data input stream enables an application to read primitive Java data types from an underlying input stream in a machine-independent way(instead of raw bytes). That is why it is called DataInputStream - because it reads data (numbers) instead of just bytes. An application uses a data output stream to write data that can later be read by a data input stream. Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8. DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class. Firstmost let us do discuss the constructor of this class ConstructorAction PerformedDataInputStream(InputStream in)Creates a DataInputStream that uses the specified underlying InputStream. Now let us discuss methods of this class that are depicted below in a tabular format as shown below as follows: Methods Action performed read(byte[] b)Reads some number of bytes from the contained input stream and stores them into the buffer array b.read(byte[] b, int off, int len)Reads up to length bytes of data from the contained input stream into an array of bytes. readBoolean()Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. readChar()Reads two input bytes and returns a char value. readUTF()Reads data from the underlying input stream and converts the bytes into a Unicode string.readByte()Read one input byte and returns a byte value.readFloat()Read four input bytes and returns a float value.readFully()Read bytes equal to the length of the byte arrayreadDouble()Reads eight input bytes and returns a double value.readInt()Reads four input bytes and returns an int value. readLine()Reading lines of textreadLong()Reading eight input bytes and returns a long valuereadShort()Read two input bytes and return a short value.readUnsignedByte()Read byte and return as an integerreadUnsignedShort()Read two input bytes and returns as an integer arrayskipBytes()Skips over n bytes of data from input stream Remember: The DataInputStream class is often used together with a DataOutputStream. Implementation: Now let us do implement a few of the above methods of this class has been discussed above Below program uses try-with-resources. It requires JDK 7 or later as concept of try-catch block was introduced in Java7 Example 1 Java // Java program to Demonstrate DataInputStream Class // Importing I/O classes import java.io.*; // Main class class DataInputStreamDemo { // Main driver method public static void main(String args[]) throws IOException { // Writing the data // Try block to check for exceptions try ( DataOutputStream dout = new DataOutputStream(new FileOutputStream("file.dat")) ) { dout.writeDouble(1.1); dout.writeInt(55); dout.writeBoolean(true); dout.writeChar('4'); } // Catch block to handle the exceptions catch (FileNotFoundException ex) { // Display message when FileNotFoundException occurs System.out.println("Cannot Open the Output File"); return; } // Reading the data back. // Try block to check for exceptions try ( DataInputStream din = new DataInputStream(new FileInputStream("file.dat")) ) { // Illustrating readDouble() method double a = din.readDouble(); // Illustrating readInt() method int b = din.readInt(); // Illustrating readBoolean() method boolean c = din.readBoolean(); // Illustrating readChar() method char d = din.readChar(); // Print the values System.out.println("Values: " + a + " " + b + " " + c + " " + d); } // Catch block to handle the exceptions catch (FileNotFoundException e) { // Display message when FileNotFoundException occurs System.out.println("Cannot Open the Input File"); return; } } } Output: Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.Next Article: Java.io.DataInputStream class in Java | Set 2 Comment More infoAdvertise with us Next Article Java.io.DataInputStream class in Java | Set 1 K kartik Improve Article Tags : Java Java-I/O Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Like