【IO】FileInputStream
前言
Github:https://github.com/yihonglei/jdk-source-code-reading(java-io)
一 FileInputStream 概述
FileInputStream为文件输入流,以字节流的形式读取文件内容,比如图片文件等等。
FileInputStream是InputStream的子类,我们可以把FileInputStream赋值给InputStream使用。
如果我们要以字符流形式读取文件内容,可以使用FileReader类处理。
二 FileInputStream 实例
FileInputStream的父类是InputStream,源码结构如下,
构造器
- public FileInputStream(File file) throws FileNotFoundException;
- public FileInputStream(FileDescriptor fdObj);
- public FileInputStream(String name) throws FileNotFoundException;
方法
- public int available() throws IOException;
- public void close() throws IOException;
- protected void finalize() throws IOException;
- public FileChannel getChannel();
- public final FileDescriptor getFD() throws IOException;
- public int read() throws IOException;
- public int read(byte[] b) throws IOException;
- public int read(byte[] b, int off, int len) throws IOException;
- public long skip(long n) throws IOException;
以下对部分重要常用方法进行分析。
read()
FileInputStream 的 read() 方法一次从文件输入流中一次读取一个字节。该方法返回读取到包含一个字节内容的int变量。
如果 read() 方法返回-1,说明程序已经读到了流的末尾,你可以关闭流。从源码 read() 方法的返回值,
我们可以知道这里的 -1 是一个 int 类型,不是 byte 类型。
哪么为啥返回 int 值呢?
因为 byte 正好是一个字节,是一个有符号的数据,不便于处理,最主要的是它无法表示是否成功读到数据。
而 int 的表值范围比较大,可用 0~255 来表示读到的字节值,而读到的若是-1,则表示没能读到数据。
package com.jpeony.io.inputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class FileInputStream1 {
public static void main(String[] args) {
// 指定构建文件
File file = new File("C:\\mycode\\hello.txt");
// 将文件构建为输入流
try (InputStream is = new FileInputStream(file)) {
StringBuilder sb = new StringBuilder();
// 读取第一个字节
int data = is.read();
// 判断是否是末尾流
while (data != -1) {
sb.append((char)data);
// 读取下一个字节
data = is.read();
}
// 输出到控制台
System.out.println("内容:" + sb.toString().getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
read(byte[] b)
FileInputStream的read(byte[] b)方法从文件输入流中读取给定数组长度的字节,存储在字节数组中。
package com.jpeony.io.inputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 读取基于字节的数据:
* 构建文件流,从输入流中读取字节数据存储到缓冲字节数组中,然后将缓冲字节数组转换成字符串打印到控制台
* FileInputStream是InputStream的子类,所以可以把FileInputStream赋值给InputStream。
* 这里使用"try-with-resources"来关闭流,也即当退出try语句块的时候,InputStream流会关闭。
*
* @author yihonglei
*/
public class FileInputStream2 {
public static void main(String[] args) {
// 指定构造文件
File file = new File("C:\\mycode\\hello.txt");
// 将文件构建为输入流
try (InputStream is = new FileInputStream(file)) {
// 创建字节数组
byte[] data = new byte[1024];
// 从文件流中读取数据,存储到缓冲字节数组
is.read(data);
// 输出文件内容
System.out.println("文件内容:" + new String(data));
} catch (IOException e) {
e.printStackTrace();
}
}
}
read(byte[] b, int off, int len)
FileInputStream的read(byte[] b, int off, int len)方法从文件输入流中offset位置,读取length长度字节,存储在字节数组中。
package com.jpeony.io.inputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileInputStream3 {
public static void main(String[] args) {
// 构建输入流
File file = new File("C:\\mycode\\hello.txt");
try (InputStream is = new FileInputStream(file)) {
// 创建字节数组
byte[] data = new byte[1024];
// 指定索引起始位置到结束位置读取流放入字节数组中
is.read(data, 0, data.length);
// 打印文件内容
System.out.println("内容:" + new String(data));
} catch (IOException e) {
e.printStackTrace();
}
}
}
还没有评论,来说两句吧...