DataInputStream in Java
In java, we use dataInputsream to read primitive data type. It read number other than bytes that are why it is called dataInputstream. In java, it is available in the Java.io package. Our java primitive class includeint, long, float etc., we read this primitive from input Stream. We use this DataInputStream in Java to read the data which is written by dataOutputStream.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
public class DataInputStream extends FilterInputStream implements DataInput{}
Above is the class declaration fordataInputsream. It implements various class and interface. Below mentioned various class and interface which are extended and implements by dataInputsream:
Classes from very parent to child sequence:
- Object
- InputStream
- FilterInputStream
- DataInputStream
Various Interfaces are below :
- AutoCloseable
- DataInput
- Closeable
How does DataInputStream work in Java?
DataInputStream in Java it has the below constructor, which takes one parameter. This parameter is nothing but our inputStream from which we are going to read the data. Below find details of how constructor works:
- DataInputStream(InputStream in): If we use this constructor to create the DataInputStream in Java object, then we need to pass the inputStream as a parameter. In java, DataInputStream extends InputStream class, so it is the subclass for InputStream, so dataInpuutStream can use all the available methods in the parent class.
- If we want to use dataInpuutStream, we first need to create the object for that:
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("your_data"));
In the above, we are creating an object for DataInputStream in Java by using its constructor where we can pass our inputStream for which we want to read our data.
- This class have the readByte() method through which we read our data.
DataInputStream ds = new DataInputStream(new FileInputStream("your_data")); ds.readByte();
- By using this method, we can read data from the above-passed inputstream. Below is the simple syntax to use dataInputStream in java:
DataInputStreamdataInputStream = new DataInputStream(newFileInputStream("file_name")); doubletoreadDouble = input.readDouble(); inttoread = input.read(); float toreadFloat = input.readFloat(); inttoreadInt = input.readInt(); input.close();
In the above simple syntax, we are getting an idea of how to use this class to read the primitive type in java (int, float, long, double etc.). Simple in the first step, we just create the object and pass our inputStream and using java in build methods to read data like int, float, double and one separate method named as read(). This DataInputStream in Java is always used with DataOuputStream as this is used to write our data. This class also contains various methods used to write data to a file. This has methods specific to int, double, float, etc. and vice versa. We also have methods to read this primitivetype data from a file.
Examples to Implement DataInputStream in Java
It has various method to read data from the input stream to read primitive type object like int, float, double, float, Boolean etc. Methods are mentioned below with example:
Example #1
longreadLong(): This method is used to read the long primitive type from the inpputstream.
Code:
package com.cont.article; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dataOut = new DataOutputStream( new FileOutputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); // here we are writing long to file. dataOut.writeLong(900); // close file. dataOut.close(); // To read data from file DataInputStream dataInputStream = new DataInputStream( new FileInputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); long longData = dataInputStream.readLong(); dataInputStream.close(); System.out.println("longData is :: = " + longData); } }
Output :
Example #2
float readFloat(): This method is used to read the float primitive type from the inpputstream file provided.
Code:
package com.cont.article; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dataOut = new DataOutputStream( new FileOutputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); // here we are writing float to file. dataOut.writeFloat(67.00F); // close file. dataOut.close(); // To read data from file (float) DataInputStream dataInputStream = new DataInputStream( new FileInputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); float floatData = dataInputStream.readFloat(); dataInputStream.close(); System.out.println("floatData is :: = " + floatData); } }
Output :
Example #3
intreadInt(): This method is used to read the int value from the inpputstream file.
Code:
package com.cont.article; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dataOut = new DataOutputStream( new FileOutputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); // here we are writing int to file. dataOut.writeInt(10); // close file. dataOut.close(); // To read data from file (int) DataInputStream dataInputStream = new DataInputStream( new FileInputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); int intData = dataInputStream.readInt(); dataInputStream.close(); System.out.println("intData is :: = " + intData); } }
Output :
Example #4
double readDouble(): This method is used to read the double primitive type from inpputstream.
Code:
package com.cont.article; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dataOut = new DataOutputStream( new FileOutputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); // here we are writing double to file. dataOut.writeDouble(1000); // close file. dataOut.close(); // To read data from file (double) DataInputStream dataInputStream = new DataInputStream( new FileInputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); double doubleData = dataInputStream.readDouble(); dataInputStream.close(); System.out.println("doubleData is :: = " + doubleData); } }
Output :
Example #5
char readChar(): This method is used to read char primitive from inpputstream. See the below example:
Code:
package com.cont.article; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dataOut = new DataOutputStream( new FileOutputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); // here we are writing char to file. dataOut.writeChar(100); // close file. dataOut.close(); // To read data from file (char) DataInputStream dataInputStream = new DataInputStream( new FileInputStream("I:\\content_article\\MAR_2020\\file\\input.bin")); char charData = dataInputStream.readChar(); dataInputStream.close(); System.out.println("charData is :: = " + charData); } }
Output :
Conclusion
DataInputStream in Java is basically used to read the data from the input stream we passed as an argument into the constructor as a file. It can read all primitive data types which are available in java. But this is not thread-safe; to provide thread safety, we need to go for others.
The above is the detailed content of DataInputStream in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
