public class FileInputStream
extends InputStream
| java.lang.Object | ||
| java.io.InputStream | ||
| java.io.FileInputStream | ||
| |
| |
FileInputStream从文件系统中的文件获取输入字节。 哪些文件可用取决于主机环境。
FileInputStream意味着读取诸如图像数据之类的原始字节流。 为了阅读字符流,请考虑使用FileReader 。
Public constructors |
|
|---|---|
FileInputStream(String name) 通过打开与文件系统中路径名称 |
|
FileInputStream(File file) 通过打开到文件系统中的实际文件(由 |
|
FileInputStream(FileDescriptor fdObj) 使用文件描述符 |
|
公共方法(Public methods) |
|
|---|---|
int |
available() 返回可从此输入流中读取(或跳过)的剩余字节数的估计值,而不会因为此输入流的下一次方法调用而被阻塞。 |
void |
close() 关闭此文件输入流并释放与该流关联的所有系统资源。 |
FileChannel |
getChannel() 返回与此文件输入流关联的唯一 |
final FileDescriptor |
getFD() 返回 |
int |
read() 从此输入流中读取一个字节的数据。 |
int |
read(byte[] b, int off, int len) 最多可将 |
int |
read(byte[] b) 从这个输入流中读取多达 |
long |
skip(long n) 跳过并丢弃来自输入流的 |
Protected methods |
|
|---|---|
void |
finalize() 确保在没有更多引用时调用此文件输入流的 |
继承方法(Inherited methods) |
|
|---|---|
java.io.InputStream
|
|
java.lang.Object
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
FileInputStream (String name)
通过打开到实际文件的连接来创建FileInputStream ,该文件由文件系统中的路径名称name命名。 将创建一个新的FileDescriptor对象来表示此文件连接。
首先,如果有安全管理器, checkRead name参数作为其参数调用其 checkRead方法。
如果指定的文件不存在,是目录而不是常规文件,或者由于其他原因无法打开读取,则会引发 FileNotFoundException 。
| 参数(Parameters) | |
|---|---|
name |
String: the system-dependent file name. |
| 抛出异常(Throws) | |
|---|---|
FileNotFoundException |
if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. |
SecurityException |
if a security manager exists and its checkRead method denies read access to the file. |
也可以看看:
FileInputStream (File file)
通过打开到文件系统中的实际文件( File对象file命名的文件)的连接来创建FileInputStream 。 将创建一个新的FileDescriptor对象来表示此文件连接。
首先,如果有安全管理器, checkRead file参数所表示的路径作为其参数调用其 checkRead方法。
如果指定的文件不存在,是目录而不是常规文件,或者由于其他原因无法打开读取,则会引发 FileNotFoundException 。
| 参数(Parameters) | |
|---|---|
file |
File: the file to be opened for reading. |
| 抛出异常(Throws) | |
|---|---|
FileNotFoundException |
if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. |
SecurityException |
if a security manager exists and its checkRead method denies read access to the file. |
FileInputStream (FileDescriptor fdObj)
使用文件描述符 fdObj创建 FileInputStream ,该文件描述符表示与文件系统中实际文件的现有连接。
如果有安全管理器,则使用文件描述符fdObj作为其参数来调用其checkRead方法,以查看是否可以读取文件描述符。 如果对文件描述符的读取访问被拒绝,则会引发SecurityException 。
如果 fdObj为空,则引发 NullPointerException 。
如果fdObj是invalid此构造函数不会引发异常。 但是,如果在生成的流上调用方法以尝试在流上进行I / O操作, IOException引发IOException 。
| 参数(Parameters) | |
|---|---|
fdObj |
FileDescriptor: the file descriptor to be opened for reading. |
int available ()
返回可从此输入流中读取(或跳过)的剩余字节数的估计值,而不会因为此输入流的下一次方法调用而被阻塞。 下一次调用可能是同一个线程或另一个线程。 单个读取或跳过这么多字节不会被阻塞,但可以读取或跳过更少的字节。
在某些情况下,非阻塞式读取(或跳过)在缓慢时可能会被阻止,例如,通过慢速网络读取大型文件时。
| 返回(Returns) | |
|---|---|
int |
an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if this file input stream has been closed by calling close or an I/O error occurs. |
void close ()
关闭此文件输入流并释放与该流关联的所有系统资源。
如果此流具有关联的频道,那么该频道也会关闭。
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
FileChannel getChannel ()
返回与此文件输入流关联的唯一 FileChannel对象。
返回通道的初始 position将等于从文件中读取的字节数。 从此流中读取字节将增加通道的位置。 通过显式或通过读取更改通道的位置将改变此流的文件位置。
| 返回(Returns) | |
|---|---|
FileChannel |
the file channel associated with this file input stream |
FileDescriptor getFD ()
返回 FileDescriptor对象,该对象表示与此 FileInputStream正在使用的文件系统中的实际文件的连接。
| 返回(Returns) | |
|---|---|
FileDescriptor |
the file descriptor object associated with this stream. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看:
int read ()
从此输入流中读取一个字节的数据。 如果没有输入可用,此方法会阻止。
| 返回(Returns) | |
|---|---|
int |
the next byte of data, or -1 if the end of the file is reached. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
int read (byte[] b,
int off,
int len)
从这个输入流中读取多达len个字节的数据到一个字节数组中。 如果len不为零,则该方法将阻塞,直到某些输入可用; 否则,不读取字节并返回0 。
| 参数(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 file 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 an I/O error occurs. |
int read (byte[] b)
从这个输入流中读取多达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 file has been reached. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
long skip (long n)
跳过并丢弃来自输入流的 n字节的数据。
由于各种原因, skip方法可能会跳过一些较小数量的字节,可能是0 。 如果n为负数, IOException抛出IOException ,即使InputStream超类的skip方法在这种情况下不会执行任何操作。 返回跳过的实际字节数。
此方法可能会跳过比备份文件中剩余的更多的字节。 这不会产生异常,并且跳过的字节数可能包含超出备份文件EOF的一些字节数。 在跳过结尾后试图从流中读取将导致-1指示文件的结尾。
| 参数(Parameters) | |
|---|---|
n |
long: the number of bytes to be skipped. |
| 返回(Returns) | |
|---|---|
long |
the actual number of bytes skipped. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if n is negative, if the stream does not support seek, or if an I/O error occurs. |
void finalize ()
确保在没有更多引用时调用此文件输入流的 close方法。
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看: