搜尋
首頁Javajava教程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 :

Java 中的資料輸入流

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 :

Java 中的資料輸入流

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 :

Java 中的資料輸入流

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 :

Java 中的資料輸入流

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 :

Java 中的資料輸入流

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中