Most visited

Recently visited

Added in API level 1

DataInputStream

public class DataInputStream
extends FilterInputStream implements DataInput

java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.FilterInputStream
       ↳ java.io.DataInputStream


数据输入流允许应用程序以独立于机器的方式从基础输入流读取原始Java数据类型。 应用程序使用数据输出流来写入数据,稍后可以通过数据输入流读取数据。

DataInputStream对于多线程访问不一定安全。 线程安全是可选的,并且是本课程中用户的责任。

也可以看看:

Summary

Inherited fields

From class java.io.FilterInputStream

Public constructors

DataInputStream(InputStream in)

创建使用指定的基础InputStream的DataInputStream。

Public methods

final int read(byte[] b)

从包含的输入流中读取一些字节数并将它们存储到缓冲区阵列 b

final int read(byte[] b, int off, int len)

从包含的输入流中最多读取 len个字节的数据到一个字节数组中。

final boolean readBoolean()

参见 readBoolean方法 DataInput的一般合同。

final byte readByte()

请参阅 readByte方法 DataInput的一般合同。

final char readChar()

请参阅 readChar方法 DataInput的一般合同。

final double readDouble()

请参阅 readDouble方法 DataInput的总体合同。

final float readFloat()

请参阅 readFloat方法 DataInput的一般合同。

final void readFully(byte[] b)

请参阅 readFully方法 DataInput的一般合同。

final void readFully(byte[] b, int off, int len)

请参阅 readFully方法 DataInput的一般合同。

final int readInt()

请参阅 readInt方法 DataInput的一般合同。

final String readLine()

此方法在API级别1中已弃用。此方法不会将字节正确转换为字符。 从JDK 1.1开始,读取文本行的首选方法是通过BufferedReader.readLine()方法。 使用DataInputStream类读取行的程序可以通过替换表单的代码转换为使用BufferedReader类:


     DataInputStream d = new DataInputStream(in);
 
with:
     BufferedReader d
          = new BufferedReader(new InputStreamReader(in));
 

final long readLong()

见的总承包 readLong的方法 DataInput

final short readShort()

参见 readShort方法 DataInput的一般合同。

final String readUTF()

请参阅 readUTF方法 DataInput的一般合同。

static final String readUTF(DataInput in)

从流in读取以modified UTF-8格式编码的Unicode字符串的表示形式; 这串字符然后以String返回。

final int readUnsignedByte()

请参阅 readUnsignedByte方法 DataInput的一般合同。

final int readUnsignedShort()

请参阅 readUnsignedShort方法 DataInput的一般合同。

final int skipBytes(int n)

见的总承包 skipBytes的方法 DataInput

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

Public constructors

DataInputStream

Added in API level 1
DataInputStream (InputStream in)

创建使用指定的基础InputStream的DataInputStream。

Parameters
in InputStream: the specified input stream

Public methods

read

Added in API level 1
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.

也可以看看:

read

Added in API level 1
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.

也可以看看:

readBoolean

Added in API level 1
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.

也可以看看:

readByte

Added in API level 1
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.

也可以看看:

readChar

Added in API level 1
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.

也可以看看:

readDouble

Added in API level 1
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.

也可以看看:

readFloat

Added in API level 1
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.

也可以看看:

readFully

Added in API level 1
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.

也可以看看:

readFully

Added in API level 1
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.

也可以看看:

readInt

Added in API level 1
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.

也可以看看:

readLine

Added in API level 1
String readLine ()

此方法在API级别1中已弃用。
此方法不能正确地将字节转换为字符。 从JDK 1.1开始,读取文本行的首选方法是通过BufferedReader.readLine()方法。 使用DataInputStream类读取行的程序可以通过替换表单的代码转换为使用BufferedReader类:

     DataInputStream d = new DataInputStream(in);
 
with:
     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.

也可以看看:

readLong

Added in API level 1
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.

也可以看看:

readShort

Added in API level 1
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.

也可以看看:

readUTF

Added in API level 1
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.

也可以看看:

readUTF

Added in API level 1
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.

也可以看看:

readUnsignedByte

Added in API level 1
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.

也可以看看:

readUnsignedShort

Added in API level 1
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.

也可以看看:

skipBytes

Added in API level 1
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.

Hooray!