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.
以上是Java 中的数据输入流的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

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

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。
