Datainputstream (binary input stream) and dataoutputstream binary output stream (Note: In is input from local files to programs, and out is output from programs to local files)

Source: Internet
Author: User

// Remember the data type that is used for output,

// For example, DOS. writeint (100); write, read: DIS. readutf () sometimes has unexpected errors, so you must always remember what data type is used for data type output reading.

 

// Datainputstream (binary input stream)

Public class datainputstreamdemo {

// Binary stream (read operation)

Public static void main (string [] ARGs ){

Fileinputstream FCM = NULL;

Datainputstream Dis = NULL;

Try {

FCM = new fileinputstream ("CBA. bin ");

Dis = new datainputstream (FCM );

// Read the string

System. Out. println (DIS. readutf ());

// Read integer

System. Out. println (DIS. readint ());

// Read bool type

System. Out. println (DIS. readboolean ());

} Catch (filenotfoundexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

} Catch (ioexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

} Finally {

Try {

Dis. Close ();

FCM. Close ();

} Catch (ioexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

}

}

}

}

 

// Dataoutputstream binary output stream

Public class dataoutputstreamdemo {
// Binary stream: Applicable to binary files
/**
* Steps for using a binary stream:
* 1. Create a byte stream
* 2. Create a binary stream
* 3. Perform read/write operations
* 4. Close the stream
*/
// For binary streams, note that the data written and read must be consistent (data type and sequence); otherwise, the data read may be incorrect.
// Binary stream (write operation)
Public static void main (string [] ARGs ){

Fileoutputstream Fos = NULL;
Dataoutputstream dos = NULL;
Try {
// 1. Create a byte stream
Fos = new fileoutputstream ("CBA. bin ");
// 2. Create a binary stream
DOS = new dataoutputstream (FOS );
// 3. write operation
Dos. writeutf ("ABC"); // write the UTF-8 encoded string to the stream.
Dos. writeint (100 );
Dos. writeboolean (true );
System. Out. println ("Write successful! ");

} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {

Try {
// 4. Close the stream
Dos. Close ();
FOS. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

}

}

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.