Source: http://www.bjsxt.com/
One, S02e158_01io_ other stream _ Basic data type +string processing stream
1, base data type +string retention data + type (for machine parsing reading)
Input stream: DataInputStream readxxx ()
Output stream: DataOutputStream writexxx ()
Package com.test.io.others;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException; /** * Data type (basic +string) Processing stream * 1, input stream DataInputStream readxxx () * 2, output stream DataOutputStream writexxx () * Use new method cannot use polymorphism * * Java.io.EOFException: The end of the file has been reached, not read the relevant content * * public class DataStream {public static void main (string[] args) throws I oexception {Write ("g:/java/test/data.txt");/Read what you don't understand: @?
d? data type Read ("G:/java/test/data.txt");
Read ("G:/java/test/path.java");//illegal content, Java.io.EOFException: Reached the end of the file, not read the relevant content byte[] data = write ();
Read (data); /** * data + type output to file * @throws IOException */public static void write (String despath) throws Ioexce
ption{ Double point = 2.5;
Long num = 100L;
String str = "data type";
Create source file des = new file (Despath); Select Stream DataOutputStream DataOutputStream dos = new DataOutputStream (New Bufferedoutputstream
(New FileOutputStream (DES)));
The order in which the operation is written must be consistent with the order of reading to prepare dos.writedouble (point);
Dos.writelong (num);
Dos.writeutf (str);//String Dos.flush ();
Dos.close (); /** * Read data from file (basic +string) + type * @throws ioexception/public static void Read (String srcpath) thr
OWS ioexception{//create source file src = new file (Srcpath); Select stream DataInputStream dis = new DataInputStream (new Bufferedinputstream (new FileInputStream) (s
RC));
The order of operation Read is consistent with writing and must exist in order to read//not consistent, data exists double point = dis.readdouble ();
Long num = Dis.readlong ();
String str = DIS.READUTF (); Dis.close (); SYSTEM.OUT.PRINTLN (Point + "," + num + "," + str ")/content: 2.5, 100, data type}/** * data + type output to byte array * @throws IOEX
Ception */public static byte[] Write () throws ioexception{//target array byte[] des = null;
Double point = 2.5;
Long num = 100L;
String str = "data type";
Select Stream DataOutputStream Bytearrayoutputstream baos = new Bytearrayoutputstream ();
DataOutputStream dos = new DataOutputStream (new Bufferedoutputstream (BAOs));
The order in which the operation is written must be consistent with the order of reading to prepare dos.writedouble (point);
Dos.writelong (num);
Dos.writeutf (str);//String Dos.flush ();
des = Baos.tobytearray ();
Baos.close ();
Dos.close ();
Return des; /** * Read data from byte array (basic +string) + type * @throws ioexception/public static void Read (byte[) src) throw s ioexception{//select stream DataInputStream dis = New DataInputStream (New Bufferedinputstream (SRC));
The order of operation Read is consistent with writing and must exist in order to read//not consistent, data exists double point = dis.readdouble ();
Long num = Dis.readlong ();
String str = DIS.READUTF ();
Dis.close (); SYSTEM.OUT.PRINTLN (Point + "," + num + "," + str ");/content: 2.5, 100, data type}}