Java的DataInputStream和DataOutputStream數(shù)據(jù)輸入輸出流
DataInputStream
DataInputStream 是數(shù)據(jù)輸入流。它繼承于FilterInputStream。
DataInputStream 是用來(lái)裝飾其它輸入流,它“允許應(yīng)用程序以與機(jī)器無(wú)關(guān)方式從底層輸入流中讀取基本 Java 數(shù)據(jù)類型”。應(yīng)用程序可以使用DataOutputStream(數(shù)據(jù)輸出流)寫(xiě)入由DataInputStream(數(shù)據(jù)輸入流)讀取的數(shù)據(jù)。
DataInputStream 函數(shù)列表:
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)
示例代碼:
關(guān)于DataInputStream中API的詳細(xì)用法:
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 和 DataOutputStream測(cè)試程序 * * @author skywang */ public class DataInputStreamTest { private static final int LEN = 5; public static void main(String[] args) { // 測(cè)試DataOutputStream,將數(shù)據(jù)寫(xiě)入到輸出流中。 testDataOutputStream() ; // 測(cè)試DataInputStream,從上面的輸出流結(jié)果中讀取數(shù)據(jù)。 testDataInputStream() ; } /** * DataOutputStream的API測(cè)試函數(shù) */ 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嚴(yán)12"); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * DataInputStream的API測(cè)試函數(shù) */ 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(); } } // 打印byte對(duì)應(yīng)的16進(jìn)制的字符串 private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); } // 打印char對(duì)應(yīng)的16進(jìn)制的字符串 private static String charToHexString(char val) { return Integer.toHexString(val); } // 打印short對(duì)應(yīng)的16進(jìn)制的字符串 private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); } }
運(yùn)行結(jié)果:
byteToHexString(0x8F):0x8f charToHexString(0x8FCF):0x8fcf readBoolean():true readByte():0x41 readChar():0x4243 readShort():0x4445 readInt():0x12345678 readLong():0xfedcba987654321 readUTF():abcdefghijklmnopqrstuvwxyz嚴(yán)12
結(jié)果說(shuō)明:
(1) 查看file.txt文本。16進(jìn)制的數(shù)據(jù)顯示如下:
001f 對(duì)應(yīng)的int值是31。它表示的含義是后面的UTF-8數(shù)據(jù)的長(zhǎng)度。字符串“abcdefghijklmnopqrstuvwxyz嚴(yán)12”中字母“ab...xyz”的長(zhǎng)度是26,“嚴(yán)”對(duì)應(yīng)的UTF-8數(shù)據(jù)長(zhǎng)度是3;“12”長(zhǎng)度是2。總的長(zhǎng)度=26+3+2=31。
(2) 返回byte對(duì)應(yīng)的16進(jìn)制的字符串
源碼如下:
private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); }
想想為什么代碼是:
return Integer.toHexString(val & 0xff);
而不是
return Integer.toHexString(val);
我們先看看 byteToHexString((byte)0x8F); 在上面兩種情況下的輸出結(jié)果。
return Integer.toHexString(val & 0xff); 對(duì)應(yīng)的輸出是“0xffffff8f”
return Integer.toHexString(val); 對(duì)應(yīng)的輸出是“0x8f”
為什么會(huì)這樣呢?
原因其實(shí)很簡(jiǎn)單,就是“byte類型轉(zhuǎn)換成int類型”導(dǎo)致的問(wèn)題。
byte類型的0x8F是一個(gè)負(fù)數(shù),它對(duì)應(yīng)的2進(jìn)制是10001111;將一個(gè)負(fù)數(shù)的byte轉(zhuǎn)換成int類型時(shí),執(zhí)行的是有符號(hào)轉(zhuǎn)型(新增位都填充符號(hào)位的數(shù)字)。0x8F的符號(hào)位是1,因?yàn)閷⑺D(zhuǎn)換成int時(shí),填充“1”;轉(zhuǎn)型后的結(jié)果(2進(jìn)制)是11111111 11111111 11111111 10001111,對(duì)應(yīng)的16進(jìn)制為0xffffff8f。
因?yàn)楫?dāng)我們執(zhí)行Integer.toHexString(val);時(shí),返回的就是0xffffff8f。
在Integer.toHexString(val & 0xff)中,相當(dāng)于0xffffff8f & 0xff,得到的結(jié)果是0x8f。
(3) 返回char和short對(duì)應(yīng)的16進(jìn)制的字符串
“返回char對(duì)應(yīng)的16進(jìn)制的字符串”對(duì)應(yīng)的源碼如下:
private static String charToHexString(char val) { return Integer.toHexString(val); }
“返回short對(duì)應(yīng)的16進(jìn)制的字符串”對(duì)應(yīng)源碼如下:
private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); }
比較上面的兩個(gè)函數(shù),為什么一個(gè)是 “val” ,而另一個(gè)是 “val & 0xffff”?
通過(guò)(2)的分析,我們類似的推出為什么 “返回short對(duì)應(yīng)的16進(jìn)制的字符串” 要執(zhí)行“val & 0xffff”。
但是,為什么 “返回char對(duì)應(yīng)的16進(jìn)制的字符串” 要執(zhí)行 “val” 即可。原因也很簡(jiǎn)單,java中char是無(wú)符號(hào)類型,占兩個(gè)字節(jié)。將char轉(zhuǎn)換為int類型,執(zhí)行的是無(wú)符號(hào)轉(zhuǎn)型,新增為都填充0。
DataOutputStream
DataOutputStream 是數(shù)據(jù)輸出流。它繼承于FilterOutputStream。
DataOutputStream 是用來(lái)裝飾其它輸出流,將DataOutputStream和DataInputStream輸入流配合使用,“允許應(yīng)用程序以與機(jī)器無(wú)關(guān)方式從底層輸入流中讀寫(xiě)基本 Java 數(shù)據(jù)類型”。
示例代碼
關(guān)于DataOutStream中API的詳細(xì)用法:
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 和 DataOutputStream測(cè)試程序 * * @author skywang */ public class DataInputStreamTest { private static final int LEN = 5; public static void main(String[] args) { // 測(cè)試DataOutputStream,將數(shù)據(jù)寫(xiě)入到輸出流中。 testDataOutputStream() ; // 測(cè)試DataInputStream,從上面的輸出流結(jié)果中讀取數(shù)據(jù)。 testDataInputStream() ; } /** * DataOutputStream的API測(cè)試函數(shù) */ 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嚴(yán)12"); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * DataInputStream的API測(cè)試函數(shù) */ 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(); } } // 打印byte對(duì)應(yīng)的16進(jìn)制的字符串 private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); } // 打印char對(duì)應(yīng)的16進(jìn)制的字符串 private static String charToHexString(char val) { return Integer.toHexString(val); } // 打印short對(duì)應(yīng)的16進(jìn)制的字符串 private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); } }
運(yùn)行結(jié)果:
byteToHexString(0x8F):0x8f charToHexString(0x8FCF):0x8fcf readBoolean():true readByte():0x41 readChar():0x4243 readShort():0x4445 readInt():0x12345678 readLong():0xfedcba987654321 readUTF():abcdefghijklmnopqrstuvwxyz嚴(yán)12
相關(guān)文章
Spring Boot 中的 @EnableDiscoveryClient 注解
@EnableDiscoveryClient 注解是 Spring Boot 應(yīng)用程序注冊(cè)到服務(wù)注冊(cè)中心的關(guān)鍵注解,這篇文章主要介紹了Spring Boot 中的 @EnableDiscoveryClient 注解,需要的朋友可以參考下2023-07-07Java獲取指定父節(jié)點(diǎn)、子節(jié)點(diǎn)的方法實(shí)現(xiàn)
在Java中,要獲取指定節(jié)點(diǎn)的父節(jié)點(diǎn)和子節(jié)點(diǎn),通常需要使用 DOM,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02詳解SpringBoot 處理異常的幾種常見(jiàn)姿勢(shì)
這篇文章主要介紹了詳解SpringBoot 處理異常的幾種常見(jiàn)姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解
這篇文章主要介紹了Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07為什么JDK8中HashMap依然會(huì)死循環(huán)
這篇文章主要介紹了為什么JDK8中HashMap依然會(huì)死循環(huán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09安裝elasticsearch-analysis-ik中文分詞器的步驟講解
今天小編就為大家分享一篇關(guān)于安裝elasticsearch-analysis-ik中文分詞器的步驟講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02