Previously written data such as double type 3.1415926, write into the file must convert the double type into a string to write into, this is a more troublesome, second, if the data is longer, The memory footprint can be large (in fact, a double type consumes 64 bits in memory and 8 bytes).
DataOutputStream and DataInputStream classes are used to solve this problem, you can write these 8 bytes directly into memory.
The DataOutputStream and DataInputStream classes are all pipelines that handle flows, both of which are wrapped on the pipe.
Operation Example
ImportJava.io.*; Public classTest { Public Static voidMain (string[] args) {Try{File F=NewFile ("E:\\java\\aaa.txt"); DataOutputStream dos=NewDataOutputStream (NewFileOutputStream (f)); String strs[]= {"Shirt", "glove", "scarf"}; floatPrices[] = {98.3f,30.3f,50.5f}; intNums[] = {3,2,1}; for(inti = 0;i < strs.length;i++) {dos.writechars (strs[i]); Dos.writechar (' \ t '); Dos.writefloat (Prices[i]); Dos.writechar (' \ t '); Dos.writeint (Nums[i]); Dos.writechar (' \ n '); } dos.close (); DataInputStream Dis=NewDataInputStream (NewFileInputStream (f)); Chartemp[]; String str; Float Price=0f; intnum = 0; CharC; Temp=New Char[100]; for(intj = 0;j < strs.length;j++){ intLen = 0; while((c = Dis.readchar ())! = ' \ t ') {Temp[len]=C; Len++; } STR=NewString (temp, 0, Len); //Dis.readchar ();? Price =dis.readfloat (); Dis.readchar (); Num=Dis.readint (); Dis.readchar (); System.out.printf ("Product Name:%s; price:%5.2f; Quantity:%d\n", Str,price,num); } //dis.close ();? No close () method? }Catch(IOException e) {e.printstacktrace (); } } }/*eofexception io Stream unexpectedly reaches end of file*/
The Java/aaa.txt file appears as follows:
Operating effect:
Data flow in Java Learning notes processing stream