| 构造方法 |
|---|
| FileInputStream(File file)
通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。 |
| FileInputStream(FileDescriptor fdObj)
创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。 |
| FileInputStream(String name)
通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。 |
| 返回类型 | 方法描述 |
|---|---|
| int | available()
返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 |
| void | close()
关闭此文件输入流并释放与流相关联的任何系统资源。 |
| protected void | finalize()
确保当这个文件输入流的 close方法没有更多的引用时被调用。 |
| FileChannel | getChannel()
返回与此文件输入流相关联的唯一的FileChannel对象。 |
| FileDescriptor | getFD()
返回表示与此 FileInputStream正在使用的文件系统中实际文件的连接的 FileDescriptor对象。 |
| int | read()
从该输入流读取一个字节的数据,达文件末尾,则返回-1 |
| int | read(byte[] b)
从该输入流读取最多 b.length个字节的数据为字节数组。 |
| int | read(byte[] b, int off, int len)
从该输入流读取最多 len字节的数据为字节数组。 |
| long | skip(long n)
跳过并从输入流中丢弃 n字节的数据。 |
拷贝图片:
//一次读多个字节,用read(byte[] b) 和 write(byte[] b, int off, int len)
FileInputStream fis = new FileInputStream("D:/tu1.jpg"); //构造读取流
FileOutputStream fos = new FileOutputStream("D:/tu2.jpg", true); //构造写入流
byte[] b = new byte[1024]; //定义缓存数组,一次读取最大1024bit
int len = 0; //每次读取字节长度
while ((len = fis.read(b)) != -1){ //-1到达文件尾部
fos.write(b,0,len); //写入指定图片流
}
fis.close(); //关闭流
fos.close();
//一个字节一个,用read(int b) 和write(int b)
FileInputStream fis = new FileInputStream("D:/tu1.jpg"); //构造读取流
FileOutputStream fos = new FileOutputStream("D:/tu2.jpg",true); //构造写入流
int d = 0; //数据缓存
while ((d = fis.read()) != -1){ //每次返回1个字节,-1到达尾部
fos.write(data); //写入指定图片流
}
fis.close(); //关闭流
fos.close();