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