java.io.FileInputStream 文件字节输入流
- 任何文件都可以采用这个流来读
- 字节的方式,完成输入的操作,完成读的操作(硬盘-->内存)
- 用read()方法一个一个字节读
一、FileInputStream的构造方法
public FileInputStream(String name) throws FileNotFoundException |
通过打开一个到实际文件的连接来创建一个FileInputStream对象,该文件通过文件系统中的路径名 name 指定。 |
public FileInputStream(File file) throws FileNotFoundException |
通过打开一个到实际文件的连接来创建一个 FileInputStreamd对象,该文件通过文件系统中的 File 对象 file 指定。 |
① FileInputStream(String name) throws FileNotFoundException
public class FileInputStreamTest01 {
//创建文件字节流对象
//文件路径,IDEA中自动\变为\\,因为java中\表示转译
//路径的另一种方式把\改成/。
//路径都是绝对路径
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("D:\\java\\temp.txt");
//fileInputStream = new FileInputStream("D:/java/temp.txt");
} catch (FileNotFoundException e) {
//文件没有找到异常
e.printStackTrace();
}finally {
//避免空指针异常
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、FileInputStream常用的方法
public int read() throws IOException |
从该输入流读取一个字节的数据。返回数据的下一个字节,如果达到文件的末尾, 返回 |
public int read(byte[] b) throws IOException |
从该输入流读取最多 返回读入缓冲区的总字节数,如果没有更多的数据,因为文件的结尾已经到达, 返回 |
public int |