-----------------------------------------------------------------------------------
FileInputStream
类声明:public class FileInputStream extends InputStream
位于java.io包下
官方对其说明:
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment..
(简单翻译:FileInputStream从文件系统中的某个文件中获得输入字节.)
构造方法:
FileInputStream(File file);
通过打开一个到实际文件的连接来创建一个FileInputStream实例,该文件通过文件系统中的File对象file指定。
FileInputStream(String name);
通过打开一个到实际文件的连接来创建一个FileInputStream实例,该文件通过文件系统中的路径名name指定。
FileInputStream(FileDescriptor fdObj);
通过使用文件描述符fdObj创建一个FileInputStream实例。
主要方法:
- int available(): 返回字节文件输入流中可读取的字节数
- void close(): 关闭此文件输入流并释放与该流有关的系统资源.
- protected void finalize(): 确保在不再引用文件输入流时调用其close()方法.
- int read(): 从文件输入流中读取一个字节数据
- int read(byte[] b): 从文件输入流中将最多b.length个字节数据读入到字节数组b中
- int read(byte[] b,int off,int len): 从文件输入流中将最多len个字节的数据读入到字节数组b中
- long skip(long n): 从文件输入流中跳过并丢弃n个字节的数据
查看源代码:
FileInputStream这个类的作用就是把文件中的内容读取到程序中去,其中最关键的就是三个read方法,其源码都是通过调用native方法来实现的。
1 public native int read() throws IOException; 2 public int read(byte b[]) throws IOException { 3 return readBytes(b, 0, b.length); 4 } 5 public int read(byte b[], int off, int len) throws IOException { 6 return readBytes(b, off, len); 7 } 8 private native int readBytes(byte b[], int off, int len) throws IOException; 9 public native long skip(long n) throws IOException; 10 public native int available() throws IOException;