The objects created by the DataInputStream and DataOutputStream classes are called data input streams and data output streams . These two streams are useful streams that allow programs to read Java raw data in a machine-independent style. In other words, when reading a value, you no longer need to care about how many bytes the value should be.
The construction methods of DataInputStream and DataOutputStream are as follows:
The created data input stream points to an underlying input stream specified by the in parameter.
The created data output stream points to an underlying output stream specified by the parameter out.
Commonly used methods of the DataInputStream and DataOutputStream classes are as follows:
For example, write several Java types of data to a file and then read it out:
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){Filefile=newFile(apple.txt);try{FileOutputStreamfos=newFileOutputStream(file);DataOutputStreamoutData=newDataOutputStream(fos);outData.writeInt(100);outData.writeLong (123456);outData.writeFloat(3.1415926f);outData.writeDouble(987654321.1234);outData.writeBoolean(true);outData.writeChars(Howareyoudoing);}catch(IOExceptione){}try{FileInputStreamfis=newFileInputStream(file);DataInputStreaminData =newDataInputStream(fis);System.out.println(inData.readInt());//Read int data System.out.println(inData.readLong());//Read long data System.out.println( +inData.readFloat());//Read float data System.out.println(inData.readDouble());//Read double data System.out.println(inData.readBoolean());//Read boolean data charc='