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:
Various Interfaces are below :
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 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.
DataInputStream ds = new DataInputStream(new FileInputStream("your_data")); ds.readByte();
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.
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:
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 :
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 :
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 :
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 :
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 :
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.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!