Java datainputstream and DataOutputStream data input and output streams _java

Source: Internet
Author: User

DataInputStream
DataInputStream is the data input stream. It inherits from FilterInputStream.
DataInputStream is used to decorate other input streams, which "allows applications to read basic Java data types from the underlying input stream in a machine-independent manner." Applications can use DataOutputStream (data output streams) to write data that is read by the DataInputStream (data input stream).
DataInputStream Function List:

DataInputStream (InputStream in)
final int  read (byte[] buffer, int offset, int length)
final int  Read ( Byte[] Final
boolean  Readboolean () Final
byte  readbyte () final
char  Readchar () Final
Double  readdouble () Final
float  readfloat () final
void  readfully (byte[] DST) Final
void  readfully (byte[] DST, int offset, int byteCount)
final int  readInt ()
final String  ReadLine () Final
long  Readlong () final short  Readshort () final
static String  readUTF (Datainput in)
final String  readUTF () final
int  readunsignedbyte ()
final int  Readunsignedshort ()
final int  skipbytes (int count)

Sample code:
For detailed usage of APIs in DataInputStream:

Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.io.FileNotFoundException;

Import java.lang.SecurityException; /** * DataInputStream and DataOutputStream Test program * * @author Skywang/public class Datainputstreamtest {private Stati

 c Final int LEN = 5;
  public static void Main (string[] args) {//test DataOutputStream, writes data to the output stream.
  Testdataoutputstream ();
  Test DataInputStream to read the data from the output stream results above.
 Testdatainputstream (); /** * DataOutputStream API Test function/private static void Testdataoutputstream () {try {File file = new file ("
   File.txt ");

   DataOutputStream out = new DataOutputStream (new FileOutputStream (file));
   Out.writeboolean (TRUE);
   Out.writebyte ((byte) 0x41);
   Out.writechar ((char) 0x4243);
   Out.writeshort (short) 0x4445); Out.writeint (0x12345678);

   Out.writelong (0x0fedcba987654321l);

   Out.writeutf ("Abcdefghijklmnopqrstuvwxyz Yan 12");
  Out.close ();
  catch (FileNotFoundException e) {e.printstacktrace ();
  catch (SecurityException e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); \/** * DataInputStream API Test function/private static void Testdatainputstream () {try {File file = new file ("
   File.txt ");

   DataInputStream in = new DataInputStream (new FileInputStream (file));
   System.out.printf ("Bytetohexstring (0x8f): 0x%s\n", bytetohexstring ((byte) 0x8f));

   System.out.printf ("Chartohexstring (0X8FCF): 0x%s\n", Chartohexstring ((char) 0X8FCF));
   System.out.printf ("Readboolean ():%s\n", In.readboolean ());
   System.out.printf ("ReadByte (): 0x%s\n", Bytetohexstring (In.readbyte ());
   System.out.printf ("Readchar (): 0x%s\n", Chartohexstring (In.readchar ());
   System.out.printf ("Readshort (): 0x%s\n", Shorttohexstring (In.readshort ()); System.out.printf ("ReadInt(): 0x%s\n ", Integer.tohexstring (In.readint ()));
   System.out.printf ("Readlong (): 0x%s\n", Long.tohexstring (In.readlong ());

   System.out.printf ("readUTF ():%s\n", In.readutf ());
  In.close ();
  catch (FileNotFoundException e) {e.printstacktrace ();
  catch (SecurityException e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }///print byte corresponding to the 16 binary string private static string Bytetohexstring (byte val) {return integer.tohexstring (Val & 0xFF
 );
 //Print char corresponds to the 16 string private static string Chartohexstring (char val) {return integer.tohexstring (val); ///print short corresponding 16 string private static string Shorttohexstring (short val) {return integer.tohexstring (Val & 0xFF
 FF);

 }
}

Run Result:

Bytetohexstring (0x8f): 0x8f
chartohexstring (0X8FCF): 0X8FCF Readboolean (
): True
readbyte (): 0x41
Readchar (): 0x4243 readshort (): 0x4445 readInt (): 0x12345678 readlong
(): 0xfedcba987654321
readUTF (): Abcdefghijklmnopqrstuvwxyz Yan 12

Results show:
(1) View the file.txt text. The 16 data is displayed as follows:

The 001f corresponding int value is 31. It means the length of the UTF-8 data that follows. String "Abcdefghijklmnopqrstuvwxyz Yan 12" in the letter ab ... XYZ "Length is 26," Yan "corresponding to the length of the UTF-8 data is 3;" 12 "Length is 2. Total length of =26+3+2=31.
(2) returns a byte corresponding to the 16-binary string
The source code is as follows:

private static String bytetohexstring (byte val) {return
 integer.tohexstring (Val & 0xff);
}

Think about why the code is:

Return Integer.tohexstring (Val & 0xff);

Instead of

Return Integer.tohexstring (Val);

Let's Look at Bytetohexstring ((byte) 0x8f) first; The output results in both cases.
Return Integer.tohexstring (Val & 0xff); The corresponding output is "0xffffff8f"
Return Integer.tohexstring (Val); The corresponding output is "0x8f"
Why is that?
The reason is really simple, which is the problem of "byte type converted to int type".
The byte type 0x8f is a negative number, and its corresponding 2 binary is 10001111; When you convert a negative byte to an int type, you perform a symbolic transition (the new digit fills the digit of the symbol bit). The symbol bit of the 0x8f is 1 because when it is converted to int, the "1" is filled, and the result (2) of the transition is 11111111 11111111 11111111 10001111, and the corresponding 16 is 0xffffff8f.
Because when we execute Integer.tohexstring (val), the return is 0xffffff8f.
In Integer.tohexstring (Val & 0xff), the equivalent 0xffffff8f & 0xFF, the result is 0x8f.
(3) Returns the 16-binary string of char and short
"Return char corresponding to the 16 string" corresponds to the source code as follows:

private static String chartohexstring (char val) {return
 integer.tohexstring (val);
}

"Return short corresponding to the 16 string" corresponds to the source code as follows:

private static String shorttohexstring (short val) {return
 integer.tohexstring (Val & 0xFFFF);
}

Compare the two functions above, why one is "Val" and the other is "Val & 0xFFFF"?
Through (2) analysis, we similar to the introduction of why "return short corresponding to the 16 string" to perform "Val & 0xFFFF".
However, why "return a char corresponding to the 16 string" is to execute "Val". The reason is also simple, in Java char is unsigned type, accounting for two bytes. Converts char to an int type, performs an unsigned transition, and the new is filled with 0.


DataOutputStream
DataOutputStream is the data output stream. It inherits from Filteroutputstream.
The dataoutputstream is used to decorate other output streams and to work with dataoutputstream and datainputstream input streams, "allowing applications to read and write basic Java data types from the underlying input stream in a machine-independent manner."
Sample code
For detailed usage of APIs in Dataoutstream:

Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.io.FileNotFoundException;

Import java.lang.SecurityException; /** * DataInputStream and DataOutputStream Test program * * @author Skywang/public class Datainputstreamtest {private Stati

 c Final int LEN = 5;
  public static void Main (string[] args) {//test DataOutputStream, writes data to the output stream.
  Testdataoutputstream ();
  Test DataInputStream to read the data from the output stream results above.
 Testdatainputstream (); /** * DataOutputStream API Test function/private static void Testdataoutputstream () {try {File file = new file ("
   File.txt ");

   DataOutputStream out = new DataOutputStream (new FileOutputStream (file));
   Out.writeboolean (TRUE);
   Out.writebyte ((byte) 0x41);
   Out.writechar ((char) 0x4243);
   Out.writeshort (short) 0x4445); Out.writeint (0x12345678);

   Out.writelong (0x0fedcba987654321l);

   Out.writeutf ("Abcdefghijklmnopqrstuvwxyz Yan 12");
  Out.close ();
  catch (FileNotFoundException e) {e.printstacktrace ();
  catch (SecurityException e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); \/** * DataInputStream API Test function/private static void Testdatainputstream () {try {File file = new file ("
   File.txt ");

   DataInputStream in = new DataInputStream (new FileInputStream (file));
   System.out.printf ("Bytetohexstring (0x8f): 0x%s\n", bytetohexstring ((byte) 0x8f));

   System.out.printf ("Chartohexstring (0X8FCF): 0x%s\n", Chartohexstring ((char) 0X8FCF));
   System.out.printf ("Readboolean ():%s\n", In.readboolean ());
   System.out.printf ("ReadByte (): 0x%s\n", Bytetohexstring (In.readbyte ());
   System.out.printf ("Readchar (): 0x%s\n", Chartohexstring (In.readchar ());
   System.out.printf ("Readshort (): 0x%s\n", Shorttohexstring (In.readshort ()); System.out.printf ("ReadInt(): 0x%s\n ", Integer.tohexstring (In.readint ()));
   System.out.printf ("Readlong (): 0x%s\n", Long.tohexstring (In.readlong ());

   System.out.printf ("readUTF ():%s\n", In.readutf ());
  In.close ();
  catch (FileNotFoundException e) {e.printstacktrace ();
  catch (SecurityException e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }///print byte corresponding to the 16 binary string private static string Bytetohexstring (byte val) {return integer.tohexstring (Val & 0xFF
 );
 //Print char corresponds to the 16 string private static string Chartohexstring (char val) {return integer.tohexstring (val); ///print short corresponding 16 string private static string Shorttohexstring (short val) {return integer.tohexstring (Val & 0xFF
 FF);

 }
}

Run Result:

Bytetohexstring (0x8f): 0x8f
chartohexstring (0X8FCF): 0X8FCF Readboolean (
): True
readbyte (): 0x41
Readchar (): 0x4243 readshort (): 0x4445 readInt (): 0x12345678 readlong
(): 0xfedcba987654321
readUTF (): Abcdefghijklmnopqrstuvwxyz Yan 12

Related Article

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.