DataInputStream class and DataOutputStream class
in the previous study, we know that data flow processing data refers to a byte or byte array, but in fact, it is often not the case, it requires the data stream can directly read, write a variety of Java language basic data types. For example, in a file to hold 300 integer values, read from, you want to press int for the basic unit to read, this can greatly save time. You need to use DataInputStream and Dataoutputstream,datainputstream and DataOutputStream not only to support methods in the parent class InputStream and OutputStream, Read and write data in the original way, while also allowing the data stream to read and write the basic data types of the Java language, including int, float, double, and double. Its constructor form is as follows:
DataInputStream (OBJ)
DataOutputStream (OBJ)
OBJ is a well-established input-output data stream object
DataInputStream and DataOutputStream are able to manipulate these primitive types because they have a specific set of methods.
The common methods of DataInputStream are as follows:
-byte ReadByte ()
long Readlong ()
double readdouble ()
, Boolean Readboolean ()
String readUTF ()
int readInt ()
< Span style= "font-size:14pt" > -> float readfloat ()
< Span style= "font-size:14pt" > & Nbsp; -> short Readshort ()
< Span style= "font-size:14pt" > Span style= "font-size:14pt" > -> char ReadChar ()
These methods read the types of int, byte, float, char, and so on, respectively.
-> void writebyte (int a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void Writelong (Long a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void wirteDouble (Double A)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void Writeboolean (Boolean a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > & nbsp; -> void writeUTF (String a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > Span style= "font-size:14pt" > -> void writeint (int a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void WriteChar (char A)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void Writefloat (float a)
< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" > -> void WriteShort ( Short a)
These methods write to the type int, byte, float , char, and so on, respectively.
The following are examples of DataInputStream and DataOutputStream:
Package com.feicuiedu.test;
Import java.io.*;
public class liu_test{
public static void Main (String args[]) throws exception{
FileOutputStream fos = new FileOutputStream ("Text.txt");
DataOutputStream dos = new DataOutputStream (FOS);
Dos.writeint (12);
Dos.writedouble (2.23d);
Dos.writeBoolean (true);
Dos.writeutf ("Java Data Flow test #");
fos.close ();
FileInputStream fis = new FileInputStream ("Text.txt");
datainputstream dis = new DataInputStream (FIS);
System.out.println (Dis.readint ());
System.out.println (Dis.readdouble ());
System.out.println (Dis.readboolean ());
System.out.println (Dis.readutf ());
Fis.close ();
} }
Output Result:
Java Fundamentals-Data flow