Java DataInputStream ClassLast Updated : 23 Jan 2026 The DataInputStream class in Java is used to read primitive data types from an input stream in a machine-independent format. In this chapter, we will learn what the DataInputStream class is, why it is used, its declaration, constructor, important methods, and how to read primitive data from a file using an example. What is DataInputStream in Java?The DataInputStream class allows a Java application to read primitive data types such as int, char, boolean, and String from an input stream in a portable way. It is generally used with the DataOutputStream class, where data written using DataOutputStream can be read correctly using DataInputStream. DataInputStream Class DeclarationThe following declaration shows how the DataInputStream class is defined: Constructor of DataInputStream ClassThe DataInputStream class provides a constructor to create a data input stream. The DataInputStream(InputStream in) constructor creates a new DataInputStream that reads data from the specified input stream. Syntax: Here is the syntax of this constructor: Example of DataInputStream Class ConstructorThe following example demonstrates how to create a DataInputStream using its constructor. Methods of DataInputStream ClassThe DataInputStream class provides methods to read primitive data types.
Example of DataInputStream ClassThis example demonstrates how to read data from a file using the DataInputStream class. Here, we are assuming that you have following data in "testout.txt" file: JAVA Output: J-A-V-A Next TopicJava FilterOutputStream Class |
We request you to subscribe our newsletter for upcoming updates.