public class FISDemo {
public static void main(String[] args) {
byte[] buf = new byte[2056];
try {
@SuppressWarnings("resource")
FileInputStream fileIn = new FileInputStream("e:/Hello.java");
int bytes = fileIn.read(buf, 0, 2056);
String str = new String(buf, 0, bytes);
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中参阅jdk文档的FileInputStream类的readd方法。
-
覆盖:
-
类
InputStream
中的read
-
参数:
-
b
- 存储读取数据的缓冲区。 -
off
- 目标数组b
中的起始偏移量。 -
len
- 读取的最大字节数。
返回:
-
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回
-1
。
抛出:
-
NullPointerException
- 如果b
为null
。 -
IndexOutOfBoundsException
- 如果off
为负、len
为负,或者len
大于b.length - off
-
IOException
- 如果发生 I/O 错误。
另请参见:
-
InputStream.read()