// 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 ();
}
}
}
}