public class DataInputStream extends FilterInputStream implements DataInput
DataInputStream对于多线程访问来说不一定是安全的。 线程安全是可选的,是本课程中用户的责任。
DataOutputStream
in| Constructor and Description |
|---|
DataInputStream(InputStream in)
创建使用指定的底层InputStream的DataInputStream。
|
| Modifier and Type | Method and Description |
|---|---|
int |
read(byte[] b)
从包含的输入流中读取一些字节数,并将它们存储到缓冲区数组
b 。
|
int |
read(byte[] b, int off, int len)
从包含的输入流读取最多
len个字节的数据为字节数组。
|
boolean |
readBoolean()
见的总承包
readBoolean的方法
DataInput 。
|
byte |
readByte()
见的总承包
readByte的方法
DataInput 。
|
char |
readChar()
见
readChar方法的总合同
DataInput 。
|
double |
readDouble()
见
readDouble方法
DataInput的总体合同。
|
float |
readFloat()
见
readFloat法
DataInput的一般合同。
|
void |
readFully(byte[] b)
见的总承包
readFully的方法
DataInput 。
|
void |
readFully(byte[] b, int off, int len)
见的总承包
readFully的方法
DataInput 。
|
int |
readInt()
见
readInt方法
DataInput的一般合同。
|
String |
readLine()
已弃用
此方法无法将字节正确转换为字符。
从JDK 1.1开始,读取文本行的
BufferedReader.readLine()方法是通过BufferedReader.readLine()方法。
使用DataInputStream类读取行的程序可以转换为使用BufferedReader类替换以下形式的代码:
与:
|
long |
readLong()
见的总承包
readLong的方法
DataInput 。
|
short |
readShort()
见
readShort方法
DataInput的一般合同。
|
int |
readUnsignedByte()
见的总承包
readUnsignedByte的方法
DataInput 。
|
int |
readUnsignedShort()
见
readUnsignedShort法
DataInput的一般合同。
|
String |
readUTF()
见
readUTF法
DataInput的一般合同。
|
static String |
readUTF(DataInput in)
从流 in读取以modified UTF-8格式编码的Unicode字符串的表示;
这个字符串然后作为String返回。
|
int |
skipBytes(int n)
见
skipBytes法
DataInput的一般合同。
|
available, close, mark, markSupported, read, reset, skippublic DataInputStream(InputStream in)
in - 指定的输入流
public final int read(byte[] b)
throws IOException
b 。
实际读取的字节数作为整数返回。
该方法阻塞直到输入数据可用,检测到文件结束或抛出异常。
如果b为null,则抛出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)
read在
FilterInputStream
b - 读取数据的缓冲区。
-1 。
IOException - 如果由于文件结尾以外的任何原因无法读取第一个字节,则流已关闭,底层输入流不支持关闭后读取,或者发生另一个I / O错误。
FilterInputStream.in ,
InputStream.read(byte[], int, int)
public final int read(byte[] b,
int off,
int len)
throws IOException
len个字节的数据到字节数组。
尝试读取多达len个字节,但可以读取较小的数字,可能为零。
实际读取的字节数作为整数返回。
该方法阻塞直到输入数据可用,检测到文件结束或抛出异常。
如果len为零,则不读取字节并返回0 ; 否则,尝试读取至少一个字节。 如果没有字节可用,因为流在文件的-1则返回值-1 ; 否则,读取至少一个字节并存储到b 。
读取的第一个字节存储在元素b[off] ,下一个字节存入b[off+1] ,依此类推。 读取的字节数最多等于len 。 令k为实际读取的字节数; 这些字节将被存储在元素b[off]通过b[off+ ķ -1] ,留下元件b[off+ ķ ]通过b[off+len-1]不受影响。
在每种情况下,元件b[0]至b[off]和元件b[off+len]至b[b.length-1]不受影响。
read在
FilterInputStream类
b - 读取数据的缓冲区。
off - 目标数组
b的起始偏移量
len - 读取的最大字节数。
-1 。
NullPointerException - 如果
b是
null 。
IndexOutOfBoundsException - 如果
off为负数,
len为负数,或
len大于
b.length - off
IOException - 如果由于文件结尾以外的任何原因无法读取第一个字节,则流已关闭,底层输入流不支持关闭后读取,或者发生另一个I / O错误。
FilterInputStream.in ,
InputStream.read(byte[], int, int)
public final void readFully(byte[] b)
throws IOException
readFully方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readFully在界面
DataInput
b - 读取数据的缓冲区。
EOFException - 如果此输入流在读取所有字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final void readFully(byte[] b,
int off,
int len)
throws IOException
readFully的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readFully在界面
DataInput
b - 读取数据的缓冲区。
off - 数据的起始偏移量。
len - 要读取的字节数。
EOFException - 如果此输入流在读取所有字节之前到达结束。
IOException - 流已关闭,包含的输入流在关闭后不支持读取,或发生另一个I / O错误。
FilterInputStream.in
public final int skipBytes(int n)
throws IOException
skipBytes方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
skipBytes在接口
DataInput
n - 要跳过的字节数。
IOException - 如果包含的输入流不支持查询,或流已关闭,并且所包含的输入流不支持关闭后读取,或者发生另一个I / O错误。
public final boolean readBoolean()
throws IOException
readBoolean方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readBoolean在接口
DataInput
boolean值读取。
EOFException - 如果此输入流已到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final byte readByte()
throws IOException
readByte方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readByte中的
DataInput
byte 。
EOFException - 如果此输入流已到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final int readUnsignedByte()
throws IOException
readUnsignedByte的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readUnsignedByte在界面
DataInput
EOFException - 如果此输入流已经到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final short readShort()
throws IOException
readShort方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readShort在接口
DataInput
EOFException - 如果此输入流在读取两个字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final int readUnsignedShort()
throws IOException
readUnsignedShort的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readUnsignedShort在接口
DataInput
EOFException - 如果此输入流在读取两个字节之前到达结束。
IOException - 流已关闭,包含的输入流在关闭后不支持读取,或发生另一个I / O错误。
FilterInputStream.in
public final char readChar()
throws IOException
readChar的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readChar中的
DataInput
char 。
EOFException - 如果此输入流在读取两个字节之前到达结束。
IOException - 流已关闭,包含的输入流在关闭后不支持读取,或发生另一个I / O错误。
FilterInputStream.in
public final int readInt()
throws IOException
readInt的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readInt中的
DataInput
int 。
EOFException - 如果此输入流在读取四个字节之前到达结束。
IOException - 流已关闭,包含的输入流在关闭后不支持读取,或发生另一个I / O错误。
FilterInputStream.in
public final long readLong()
throws IOException
readLong法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readLong在接口
DataInput
long 。
EOFException - 如果此输入流在读取八个字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
FilterInputStream.in
public final float readFloat()
throws IOException
readFloat方法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readFloat在界面
DataInput
float 。
EOFException - 如果此输入流在读取四个字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
readInt() ,
Float.intBitsToFloat(int)
public final double readDouble()
throws IOException
readDouble法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readDouble在界面
DataInput
double 。
EOFException - 如果此输入流在读取八个字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
readLong() ,
Double.longBitsToDouble(long)
@Deprecated public final String readLine() throws IOException
BufferedReader.readLine()方法是通过BufferedReader.readLine()方法。
使用DataInputStream类读取行的程序可以转换为使用BufferedReader类替换以下形式的代码:
DataInputStream d = new DataInputStream(in);
与:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));
readLine的方法DataInput 。
从所包含的输入流中读取此操作的字节数。
readLine在界面
DataInput
IOException - 如果发生I / O错误。
BufferedReader.readLine() ,
FilterInputStream.in
public final String readUTF() throws IOException
readUTF法DataInput的一般合同。
从所包含的输入流中读取此操作的字节数。
readUTF在接口
DataInput
EOFException - 如果此输入流在读取所有字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或发生另一个I / O错误。
UTFDataFormatException - 如果字节不表示字符串的有效修改的UTF-8编码。
readUTF(java.io.DataInput)
public static final String readUTF(DataInput in) throws IOException
in读取一个Unicode字符串表示形式编码为modified UTF-8格式;
这个字符串然后作为String返回。
修改的UTF-8表示的细节与DataInput的readUTF方法DataInput 。
in - 数据输入流。
EOFException - 如果输入流在所有字节之前到达结束。
IOException - 流已关闭,包含的输入流不支持关闭后读取,或者发生另一个I / O错误。
UTFDataFormatException - 如果字节不表示Unicode字符串的有效修改的UTF-8编码。
readUnsignedShort()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.