public class DataInputStream
extends FilterInputStream
implements DataInput
java.lang.Object | |||
↳ | java.io.InputStream | ||
↳ | java.io.FilterInputStream | ||
↳ | java.io.DataInputStream |
数据输入流允许应用程序以独立于机器的方式从基础输入流读取原始Java数据类型。 应用程序使用数据输出流来写入数据,稍后可以通过数据输入流读取数据。
DataInputStream对于多线程访问不一定安全。 线程安全是可选的,并且是本课程中用户的责任。
也可以看看:
Inherited fields |
---|
From class java.io.FilterInputStream
|
Public constructors |
|
---|---|
DataInputStream(InputStream in) 创建使用指定的基础InputStream的DataInputStream。 |
Public methods |
|
---|---|
final int |
read(byte[] b) 从包含的输入流中读取一些字节数并将它们存储到缓冲区阵列 |
final int |
read(byte[] b, int off, int len) 从包含的输入流中最多读取 |
final boolean |
readBoolean() 参见 |
final byte |
readByte() 请参阅 |
final char |
readChar() 请参阅 |
final double |
readDouble() 请参阅 |
final float |
readFloat() 请参阅 |
final void |
readFully(byte[] b) 请参阅 |
final void |
readFully(byte[] b, int off, int len) 请参阅 |
final int |
readInt() 请参阅 |
final String |
readLine() 此方法在API级别1中已弃用。此方法不会将字节正确转换为字符。 从JDK 1.1开始,读取文本行的首选方法是通过 with:DataInputStream d = new DataInputStream(in); BufferedReader d = new BufferedReader(new InputStreamReader(in)); |
final long |
readLong() 见的总承包 |
final short |
readShort() 参见 |
final String |
readUTF() 请参阅 |
static final String |
readUTF(DataInput in) 从流 |
final int |
readUnsignedByte() 请参阅 |
final int |
readUnsignedShort() 请参阅 |
final int |
skipBytes(int n) 见的总承包 |
Inherited methods |
|
---|---|
From class java.io.FilterInputStream
|
|
From class java.io.InputStream
|
|
From class java.lang.Object
|
|
From interface java.io.Closeable
|
|
From interface java.io.DataInput
|
|
From interface java.lang.AutoCloseable
|
DataInputStream (InputStream in)
创建使用指定的基础InputStream的DataInputStream。
Parameters | |
---|---|
in |
InputStream : the specified input stream |
int read (byte[] b)
从包含的输入流中读取一些字节数并将它们存储到缓冲区阵列b
。 实际读取的字节数作为整数返回。 此方法阻塞,直到输入数据可用,检测到文件结尾或引发异常。
如果b
为空,则引发NullPointerException
。 如果b
的长度为零,则不读取任何字节并返回0
; 否则,尝试读取至少一个字节。 如果由于流在文件末尾而没有可用的字节,则返回值-1
; 否则,至少读取一个字节并存储到b
。
读取的第一个字节存储到元素b[0]
,下一个存储到b[1]
,依此类推。 读取的字节数最多等于b
的长度。 让k
为实际读取的字节数; 这些字节将存储在元素b[0]
到b[k-1]
,使元素b[k]
到b[b.length-1]
不受影响。
read(b)
方法具有与以下相同的效果:
read(b, 0, b.length)
Parameters | |
---|---|
b |
byte : the buffer into which the data is read. |
Returns | |
---|---|
int |
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. |
Throws | |
---|---|
IOException |
if the first byte cannot be read for any reason other than end of file, the stream has been closed and the underlying input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
int read (byte[] b, int off, int len)
从包含的输入流中读取多达len
个字节的数据字节数组。 尝试读取多达len
个字节,但可以读取较小的数字,可能为零。 实际读取的字节数作为整数返回。
此方法阻塞,直到输入数据可用,检测到文件结尾或引发异常。
如果len
为零,则不读取字节并返回0
; 否则,尝试读取至少一个字节。 如果因为流在文件结尾而没有可用的字节,则返回值-1
; 否则,至少读取一个字节并存储到b
。
读取的第一个字节存储到元素b[off]
,下一个存储到b[off+1]
,依此类推。 读取的字节数最多等于len
。 令k为实际读取的字节数; 这些字节将存储在元素b[off]
到b[off+
k -1]
,从而使元素b[off+
k ]
到b[off+len-1]
不受影响。
在每种情况下,元素 b[0]
到 b[off]
和元素 b[off+len]
到 b[b.length-1]
都不受影响。
Parameters | |
---|---|
b |
byte : the buffer into which the data is read. |
off |
int : the start offset in the destination array b |
len |
int : the maximum number of bytes read. |
Returns | |
---|---|
int |
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. |
Throws | |
---|---|
NullPointerException |
If b is null . |
IndexOutOfBoundsException |
If off is negative, len is negative, or len is greater than b.length - off |
IOException |
if the first byte cannot be read for any reason other than end of file, the stream has been closed and the underlying input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
boolean readBoolean ()
请参阅 readBoolean
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
boolean |
the boolean value read. |
Throws | |
---|---|
EOFException |
if this input stream has reached the end. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
byte readByte ()
请参阅 readByte
方法 DataInput
的总体合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
byte |
the next byte of this input stream as a signed 8-bit byte . |
Throws | |
---|---|
EOFException |
if this input stream has reached the end. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
char readChar ()
请参阅 readChar
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
char |
the next two bytes of this input stream, interpreted as a char . |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading two bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
double readDouble ()
请参阅 readDouble
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
double |
the next eight bytes of this input stream, interpreted as a double . |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading eight bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
float readFloat ()
请参阅 readFloat
方法 DataInput
的总体合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
float |
the next four bytes of this input stream, interpreted as a float . |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading four bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
void readFully (byte[] b)
见的总承包 readFully
的方法 DataInput
。
从包含的输入流中读取此操作的字节。
Parameters | |
---|---|
b |
byte : the buffer into which the data is read. |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading all the bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
void readFully (byte[] b, int off, int len)
请参阅 readFully
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Parameters | |
---|---|
b |
byte : the buffer into which the data is read. |
off |
int : the start offset of the data. |
len |
int : the number of bytes to read. |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading all the bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
int readInt ()
请参阅 readInt
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
int |
the next four bytes of this input stream, interpreted as an int . |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading four bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
String readLine ()
此方法在API级别1中已弃用。
此方法不能正确地将字节转换为字符。 从JDK 1.1开始,读取文本行的首选方法是通过BufferedReader.readLine()
方法。 使用DataInputStream
类读取行的程序可以通过替换表单的代码转换为使用BufferedReader
类:
with:DataInputStream d = new DataInputStream(in);
BufferedReader d = new BufferedReader(new InputStreamReader(in));
见的总承包 readLine
的方法 DataInput
。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
String |
the next line of text from this input stream. |
Throws | |
---|---|
IOException |
if an I/O error occurs. |
也可以看看:
long readLong ()
参见 readLong
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
long |
the next eight bytes of this input stream, interpreted as a long . |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading eight bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
short readShort ()
参见 readShort
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
short |
the next two bytes of this input stream, interpreted as a signed 16-bit number. |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading two bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
String readUTF ()
见的总承包 readUTF
的方法 DataInput
。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
String |
a Unicode string. |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading all the bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
UTFDataFormatException |
if the bytes do not represent a valid modified UTF-8 encoding of a string. |
也可以看看:
String readUTF (DataInput in)
从流in
读取以modified UTF-8格式编码的Unicode字符串的表示形式; 这串字符然后以String
返回。 修改后的UTF-8表示的细节与readUTF
方法DataInput
。
Parameters | |
---|---|
in |
DataInput : a data input stream. |
Returns | |
---|---|
String |
a Unicode string. |
Throws | |
---|---|
EOFException |
if the input stream reaches the end before all the bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
UTFDataFormatException |
if the bytes do not represent a valid modified UTF-8 encoding of a Unicode string. |
也可以看看:
int readUnsignedByte ()
请参阅 readUnsignedByte
方法 DataInput
的一般合同。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
int |
the next byte of this input stream, interpreted as an unsigned 8-bit number. |
Throws | |
---|---|
EOFException |
if this input stream has reached the end. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
int readUnsignedShort ()
见的总承包 readUnsignedShort
的方法 DataInput
。
从包含的输入流中读取此操作的字节。
Returns | |
---|---|
int |
the next two bytes of this input stream, interpreted as an unsigned 16-bit integer. |
Throws | |
---|---|
EOFException |
if this input stream reaches the end before reading two bytes. |
IOException |
the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |
也可以看看:
int skipBytes (int n)
见的总承包 skipBytes
的方法 DataInput
。
从包含的输入流中读取此操作的字节。
Parameters | |
---|---|
n |
int : the number of bytes to be skipped. |
Returns | |
---|---|
int |
the actual number of bytes skipped. |
Throws | |
---|---|
IOException |
if the contained input stream does not support seek, or the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs. |