DataInputStream
DataInputStream is a 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 to write data read by DataInputStream.
DataInputStream function list:
DataInputStream(InputStream in)final int read(byte[] buffer, int offset, int length)final int read(byte[] buffer)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:
About the detailed usage of API 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 static final int LEN = 5; public static void main(String[] args) { // Test DataOutputStream and write data to the output stream. testDataOutputStream() ; // Test DataInputStream to read data from the above output stream results. 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 Yan12"); 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 the hexadecimal string corresponding to byte private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); } // Print the hexadecimal string corresponding to char private static String charToHexString(char val) { return Integer.toHexString(val); } // Print the hexadecimal string corresponding to short private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); }} Running results:
byteToHexString(0x8F):0x8fcharToHexString(0x8FCF):0x8fcfreadBoolean():truereadByte():0x41readChar():0x4243readShort():0x4445readInt():0x12345678readLong():0xfedcba987654321readUTF():abcdefghijklmnopqrstuvwxyz Yan12
Results description:
(1) View file.txt text. The hexadecimal data is displayed as follows:
The corresponding int value of 001f is 31. It means the length of the UTF-8 data that follows. The length of the letter "abcdefghijklmnopqrstuvwxyz Yan12" is 26, and the length of the UTF-8 data corresponding to "门" is 3; the length of "12" is 2. Total length = 26+3+2=31.
(2) Return the hexadecimal string source code corresponding to byte 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
return Integer.toHexString(val);
Let's first look at the output results of byteToHexString((byte)0x8F); in the above two cases.
return Integer.toHexString(val & 0xff); The corresponding output is "0xffffff8f"
return Integer.toHexString(val); The corresponding output is "0x8f"
Why is this happening?
The reason is actually very simple, it is the problem caused by "converting byte type to int type".
0x8F of the byte type is a negative number, and its corresponding binary is 10001111; when converting a negative number byte to an int type, signed transformation is performed (new bits are filled with the number of signed bits). The sign bit of 0x8F is 1, because when converting it to int, it is filled with "1"; the result after transformation (binary) is 11111111111111111111111111111111111111111111111111111111111110011111, and the corresponding hexadecimal is 0xffffff8f.
Because when we execute Integer.toHexString(val);, the returned is 0xffffff8f.
In Integer.toHexString(val & 0xff), it is equivalent to 0xffffff8f & 0xff, and the result is 0x8f.
(3) Return the hexadecimal string corresponding to char and short. The source code corresponding to "return the hexadecimal string corresponding to char" is as follows:
private static String charToHexString(char val) { return Integer.toHexString(val);} "Return the hexadecimal string corresponding to short" corresponds to the source code as follows:
private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff);} Comparing the two functions above, why one is "val" and the other is "val & 0xffff"?
Through the analysis of (2), we similarly deduce why "return the hexadecimal string corresponding to short" is to execute "val & 0xffff".
However, why do you need to execute "val" if you "return the hexadecimal string corresponding to char". The reason is also very simple. In Java, char is an unsigned type, accounting for two bytes. Convert char to int type, performs unsigned transformation, and adds all populated with 0.
DataOutputStream
DataOutputStream is the data output stream. It inherits from FilterOutputStream.
DataOutputStream is used to decorate other output streams, using 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 API 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 static final int LEN = 5; public static void main(String[] args) { // Test DataOutputStream and write data to the output stream. testDataOutputStream() ; // Test DataInputStream to read data from the above output stream results. 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 Yan12"); 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 the hexadecimal string corresponding to byte private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); } // Print the hexadecimal string corresponding to char private static String charToHexString(char val) { return Integer.toHexString(val); } // Print the hexadecimal string corresponding to short private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); }} Running results:
byteToHexString(0x8F):0x8fcharToHexString(0x8FCF):0x8fcfreadBoolean():truereadByte():0x41readChar():0x4243readShort():0x4445readInt():0x12345678readLong():0xfedcba987654321readUTF():abcdefghijklmnopqrstuvwxyz Yan12