Data flow in Java:
For a question: Is there a way to write a long type of data to a file?
Go to string → write through GetBytes (), laborious, and in this process the number of long types need to be constantly converted.
Now, the data flow in Java can solve this problem very well (no need to convert, write directly in)
1. DataInputStream and DataOutputStream inherit from InputStream, OutputStream respectively, it belongs to the processing flow, need to be nested in InputStream, OutputStream type of node flow above .
2. DataInputStream and DataOutputStream provide methods that can access machine-independent Java primitive type data (such as int, double, etc.).
3. DataInputStream and DataOutputStream are constructed by:
DataInputStream (InputStream in)
DataOutputStream (Outputstram out)
"Note" UTF (Unicode string): Often used on the network; UTF-8 than UTF-16.
Demo_1:
Import Java.io.*;class Test {public static void main (string[] args) {Bytearrayoutputstream BAOs = new Bytearrayoutputstrea M (); First allocate a byte array on top of the memory, and a pipe is connected (above a piece of memory) dataoutputstream dos = new DataOutputStream (BAOs); BAOs pipe is not easy to use, the outside that again set up a pipe dostry {dos.writedouble (Math.random ());d Os.writeboolean (false); Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ()); The reference System.out.println (Bais.available ()) about this array can be obtained by BAOs's Tobytearray (); How many bytes in the Bais are available for reading; DataInputStream dis = new DataInputStream (Bais); What is inside (string) when reading from a byte array? ... Not clear), it is necessary to set up a layer of pipe System.out.println (dis.readdouble ()); Write first read System.out.println (Dis.readboolean ());d os.close (), Baos.close ();d is.close (); Bais.close ();} catch (IOException e) {e.printstacktrace ();}}}
Operation Result:
9
0.1088353250890215
False
Data flow of I/O streams in Java