JAVA IO operations: Data manipulation Flow: DataOutputStream and DataInputStream

Source: Internet
Author: User

Mastering the role of DataOutputStream and DataInputStream.

You can write and read data using DataOutputStream and DataInputStream.

provides two platform-agnostic data manipulation flows in IO packages

Data output stream: DataOutputStream

Data input stream: DataInputStream

The input and output is usually entered in a certain format, and then the data is entered in a certain format.

To use the data output stream and the input stream, it is certain that the user specifies the format in which the data is saved . Data must be saved in the specified format before the data input stream can be read in.

DataOutputStream

Dataoutputstreams is a subclass of OutputStream.

 Public class Filteroutputstream DataOutput

This class inherits from the Fillteroutputstream class and implements the DataOutput interface at the same time. The DataOutput interface defines a series of methods for writing various data.

The DataOutput interface defines a series of writexxx () operations that can be written to various types of data.

If you want to write data using DataOutputStream, you must specify the output format of the good data.

Each row of data, through the ' n\ ' end, each row of data through the "\ T" end.

code example:

 Packageclass set;ImportJava.io.DataOutputStream;ImportJava.io.File;ImportJava.io.FileOutputStream; Public classdataoutputstreamdemo{ Public Static voidMain (String args[])throwsexception{//all exceptions ThrownDataOutputStream dos =NULL;//declaring a data output stream objectFile f =NewFile ("D:" + File.separator + "Order.txt");//save path to fileDOS =NewDataOutputStream (new FileOutputStream (f)) ;//instantiating a data output stream objectString names[] = {"Shirt", "glove", "scarf"};//Product Name        floatPrices[] = {98.3f,30.3f,50.5f};//Commodity price        intNums[] = {3,2,1};//Number of goods         for(inti=0;i<names.length;i++) {//Loop OutputDos.WriteChars(Names[i]);//write the string, Note that there are few writechars () here, not Writechar (). Dos.Writechar(' \ t ');//write the delimiter, this side is read Writechar (). Dos.writefloat(Prices[i]);//Write PriceDos.writechar (' \ t ');//Write delimiterDos.writeint (Nums[i]);//Number of writesDos.writechar (' \ n ');//line Break} dos.close (); //turn off the output stream    }};

Code-generated files:

Data written using Dataoutpputstream is removed using DataInputStream.

DataInputStream
 Packageclass set;ImportJava.io.DataInputStream;ImportJava.io.File;ImportJava.io.FileInputStream; Public classdatainputstreamdemo{ Public Static voidMain (String args[])throwsexception{//all exceptions ThrownDataInputStream dis =NULL;//declaring data input stream objectsFile f =NewFile ("D:" + File.separator + "Order.txt");//save path to filedis =NewDataInputStream (new FileInputStream (f)) ;//instantiating data input stream objects         String Name =NULL;//Receive name        floatPrice = 0.0f;//Receive Price        intnum = 0;//Received Quantity        char temp[] = null; Receive Product name        intlen = 0;//Save the number of read data        Charc = 0;//' \u0000 '        Try{             while(true) {Temp=New Char[200];//Open SpaceLen = 0 ;  while(C=dis.ReadChar ()) = ' \ t ') {//The content is received because the reading of  A string is not read until ' \ t ' is read, and there is no content to indicate. Temp[len] =C; Len++ ;//read length plus 1} name=new String (Temp,0,len);//Change a character array to a stringPrice = dis.readfloat ();    Read Price Dis.readchar (); Read \ t                num = Dis.readint ();//Read int               Dis.readchar (); Read \ nSystem.out.printf ("Name:%s; price:%5.2f; Quantity:%d\n", Name,price,num); }        }Catch(Exception e) {} dis.close (); }};

Execution Result:

Name: Shirts; Price: 98.30; Quantity: 3 name: glove; Price:30.30; Quantity: 2 name: scarf; Price:50.50; Quantity: 1

Visible:

DataInputStream read the string, only through Readchar (), read one by one, not like Dataoutpputstream also distinguish WriteChars (), Writechar ().

Summarize:

Two interfaces:

DataOutput interface and Datainput interface. Two methods of data manipulation flow are methods that inherit two interfaces, and the operations of the two interfaces correspond to each other and are used later.

JAVA IO operations: Data manipulation Flow: DataOutputStream and DataInputStream

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.