JAVA DataOutputStream and DataInputStream

Source: Internet
Author: User

is an ornament class for input and output streams in Java

The dataoutputstream Data output stream allows the application to write the basic Java data type in an appropriate manner to the output stream. The application can then use the data input stream to read the data in.

Main methods:

void Write (byte[] b,int off,int len);//Writes Len Byte of the byte array starting with the off angle to the OutputStream output stream object.

void write (int b);//writes the minimum 8 bits of the specified byte to the underlying output stream.

void Writeboolean (Boolean b);//Writes a Boolean value in 1-byte form to the basic output stream.

void WriteByte (int v);//Writes a byte value to the basic output stream as a 1-byte value.

void Writebytes (string s);//Writes the string in byte order to the basic output stream.

void Writechar (int v);//Writes a char value in 2-byte form to the basic output stream. Write high bytes first.

void Writeint (int v);//writes an int value as 4-byte value to the output stream before writing high bytes.

void writeUTF (String str);//Writes a string to the basic output stream in a machine-independent way with the UTF-8 revision. The method first writes two bytes with Writeshort to indicate the number of bytes later.

int size ();//Returns the current value of written

DataInputStream allows applications to read basic Java data types from the underlying input stream in a machine-independent manner. Applications can use data output streams to write data that is read by the data input stream.

Main methods:

int read (byte[] b);//reads a certain byte from the input stream and stores it in buffer array B. Returns the total number of bytes in the buffer.

int read (byte[] buf,int off,int len);//reads the Len bytes from the input stream one at a time in the byte array, with the offset off byte and the trailing position.

String readUTF ();//Read into a string that has been encoded using UTF-8 modified format

String ReadLine ();

Boolean Readboolean;

int readInt ();

byte ReadByte ();

Char ReadChar ();

Note: DataInputStream and DataOutputStream must be in pairs to appear datainputstream read data, need and DataOutputStream write in the same order, nor skip reading

 Packageio;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classdataiotest{ Public Static voidMain (string[] args) {writedata ();    ReadData (); }    Private Static voidWriteData () {DataOutputStream dos=NULL; Try{dos=NewDataOutputStream (NewFileOutputStream (NewFile ("Dataoutput.txt"))); } Catch(FileNotFoundException E1) {e1.printstacktrace (); }        Try{dos.writeint (123); Dos.writebyte (C); Dos.writedouble (12.345); Dos.writefloat ((float) 1.23); Dos.writeutf ("This is a string");        Dos.flush (); } Catch(IOException e) {e.printstacktrace (); } finally        {            if(NULL!=dos) {                Try{dos.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }    Private Static voidReadData () {DataInputStream dis=NULL; Try{dis=NewDataInputStream (NewFileInputStream (NewFile ("Dataoutput.txt"))); } Catch(FileNotFoundException e) {e.printstacktrace (); }        Try{System.out.println (Dis.readint ()); System.out.println ((Char) Dis.readbyte ());            System.out.println (Dis.readdouble ());            System.out.println (Dis.readfloat ());        System.out.println (Dis.readutf ()); } Catch(IOException e) {e.printstacktrace (); } finally        {            if(NULL!=dis) {                Try{dis.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }}

JAVA 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.