數(shù)據(jù)輸入流可以使一個(gè)應(yīng)用程序以與機(jī)器無(wú)關(guān)的方式從基本輸入流中讀取 Java 的基本數(shù)據(jù)類型。應(yīng)用程序使用一個(gè)數(shù)據(jù)輸出流輸出數(shù)據(jù),以后可使用一個(gè)數(shù)據(jù)輸入流讀入。
數(shù)據(jù)輸入流和數(shù)據(jù)輸出流以稍加修訂的 UTF-8 格式表示 Unicode 字符串。 (關(guān)于更詳細(xì)信息,請(qǐng)參看 X/Open Company Ltd., “文件系統(tǒng)安全 UCS 轉(zhuǎn)換格式 (FSS_UTF)”, X/Open 初步規(guī)范, 文檔號(hào):P316。這個(gè)信息也出現(xiàn)在 ISO/IEC 10646, Annex P。)
所有在 '\u0001'
到
'\u007F'
之間的字符被表示為一個(gè)單字節(jié):
0 | 位 0-7 |
Null 字符 '\u0000'
和從
'\u0080'
到 '\u07FF'
之間的字符被表示為兩個(gè)字節(jié):
1 | 1 | 0 | 位 6-10 |
1 | 0 | 位 0-5 |
'\u0800'
到
'\uFFFF'
之間的字符被表示為三個(gè)字節(jié):
1 | 1 | 1 | 0 | 位 12-15 |
1 | 0 | 位 6-11 | ||
1 | 0 | 位 0-5 |
這種格式與“標(biāo)準(zhǔn)的”UTF-8 格式的區(qū)別如下:
'\u0000'
用兩個(gè)字節(jié)而不是一個(gè)字節(jié)格式編碼,所以編碼后的字符串永不會(huì)包含空字符。
java.lang.Object | +----java.io.InputStream | +----java.io.FilterInputStream | +----java.io.DataInputStream
byte.length
個(gè)字節(jié)數(shù)據(jù)讀到一個(gè)字節(jié)數(shù)組中。
len
個(gè)字節(jié)數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組中。
boolean
值。
double
值。
float
值。
b.length
個(gè)字節(jié)到該字節(jié)數(shù)組。
len
個(gè)字節(jié)到該字節(jié)數(shù)組中。
n
字節(jié)。
public DataInputStream(InputStream in)
public final int read(byte b[]) throws IOException
byte.length
個(gè)字節(jié)數(shù)據(jù)讀到一個(gè)字節(jié)數(shù)組中。 這個(gè)方法將阻塞直到有輸入數(shù)據(jù)可用。
DataInputStream
的read
方法,用三個(gè)參數(shù) b
, 0
和 b.length
調(diào)用它的基本輸入流的具有三個(gè)參數(shù)的 read
方法, 且將它的返回值返回。
-1
。 public final int read(byte b[], int off, int len) throws IOException
len
個(gè)字節(jié)數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組中。 這個(gè)方法將阻塞直到有輸入數(shù)據(jù)可用。
DataInputStream
的 read
方法,用相同參數(shù)調(diào)用它的基本輸入流中的 read
方法, 且將此方法的返回值返回。
-1
。 public final void readFully(byte b[]) throws IOException
b.length
個(gè)字節(jié)到該字節(jié)數(shù)組。 這個(gè)方法將重復(fù)從基本流中讀取數(shù)據(jù),直到讀入所有的字節(jié)。 該方法將一直阻塞,直到所有的字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final void readFully(byte b[], int off, int len) throws IOException
len
個(gè)字節(jié)到該字節(jié)數(shù)組中。 這個(gè)方法將重復(fù)從基本流中讀取數(shù)據(jù),直到讀入所有的字節(jié)。 該方法將一直阻塞,直到所有的字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final int skipBytes(int n) throws IOException
n
個(gè)字節(jié)。該方法將一直阻塞,直到所有的字節(jié)數(shù)據(jù)被跳過,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
n
。
public final boolean readBoolean() throws IOException
boolean
值。 此方法從基本輸入流中讀入一個(gè)單字節(jié)。 0
表示 false
。 任一其它值表示 true
。 該方法將一直阻塞,直到該字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
boolean
值。
public final byte readByte() throws IOException
b
, 且
0 <= b <= 255
,
那么結(jié)果是:
(byte)(b)
該方法將一直阻塞,直到該字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
byte
的下一個(gè)字節(jié)。
public final int readUnsignedByte() throws IOException
public final short readShort() throws IOException
b1
和
b2
, 均在
0
和 255
之間, 那么結(jié)果等于:
(short)((b1 << 8) | b2)
該方法將一直阻塞,直到此兩個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final int readUnsignedShort() throws IOException
b1
和
b2
, 滿足 0 <= b1, b2 <= 255
,
那么結(jié)果等于:
(b1 << 8) | b2
該方法將一直阻塞,直到此兩個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final char readChar() throws IOException
b1
和
b2
, 滿足 0 <= b1, b1 <= 255
,
那么結(jié)果等于:
(char)((b1 << 8) | b2)
該方法將一直阻塞,直到此兩個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final int readInt() throws IOException
b1
,
b2
,b3
和 b4
, 滿足
0 <= b1, b2,b3,b4 <= 255
,
那么結(jié)果等于:
(b1 << 24) | (b2 << 16) + (b3 << 8) +b4
該方法將一直阻塞,直到此四個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
int
。
public final long readLong() throws IOException
b1
, b2
, b3
,
b4
, b5
, b6
,
b7
, 和 b8,
當(dāng):
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255,
相應(yīng)的結(jié)果等于:
((long)b1 << 56) + ((long)b2 << 48) + ((long)b3 << 40) + ((long)b4 << 32) + ((long)b5 << 24) + (b6 << 16) + (b7 << 8) + b8
該方法將一直阻塞,直到此八個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
long
。
public final float readFloat() throws IOException
float
值。 這個(gè)方法與
readInt
方法一樣讀取一個(gè) int
值,然后使用類
Float
中 intBitsToFloat
方法將這個(gè)
int
轉(zhuǎn)換為一個(gè) float
值。
該方法將一直阻塞,直到此四個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
float
。
public final double readDouble() throws IOException
double
值。 這個(gè)方法同
readLong
方法一樣讀取一個(gè)
long
值,然后使用類
Double
中的 longBitsToDouble
方法將這個(gè) long
轉(zhuǎn)換為一個(gè) double
。
該方法將一直阻塞,直到此八個(gè)字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
double
。
public final String readLine() throws IOException
BufferedReader.readLine()
方法。 使用類
DataInputStream
讀取行的程序,能轉(zhuǎn)換為使用
類 BufferedReader
的代碼,通過置換以下代碼
DataInputStream d = new DataInputStream(in);
BufferedReader d
= new BufferedReader(new InputStreamReader(in));
一個(gè)文本行結(jié)束于一個(gè)回車符('\r'
), 一個(gè)換行符
('\n'
), 一個(gè)回車符緊跟一個(gè)換行符或輸入流的末尾。 如果有行結(jié)束符,則它將作為返回串的一部分。
該方法將一直阻塞,直到讀取了一個(gè)換行符、一個(gè)回車符、或緊跟回車符的換行符,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public final String readUTF() throws IOException
readUTF(this)
。關(guān)于此格式的更完整描述請(qǐng)參見 readUTF(java.io.DataInput)
。
該方法將一直阻塞,直到所有的字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。
public static final String readUTF(DataInput in) throws IOException
前兩個(gè)字節(jié)以同 readUnsignedShort
方法一樣的機(jī)制被讀入。這個(gè)值給出了編碼字符串中剩余的字節(jié)數(shù)(注:不是結(jié)果字符串的長(zhǎng)度)。然后剩余的字節(jié)被看作是已用 UTF-8 格式編碼的字符,并且被轉(zhuǎn)換為字符。
該方法將一直阻塞,直到所有的字節(jié)數(shù)據(jù)被讀入,或檢測(cè)到了數(shù)據(jù)流尾或拋出異常。