Data flow in Java Learning notes processing stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.