一.概念
FileInputStream和FileOutputStream 是一对继承与InputStream和OutputStream的类,用于本地文件读写,按二进制格式读写并且顺序读写,读和写的文件流要区分开,即分别创建不同文件流对象。
二.记住in和out
死记硬背型:
不管你从磁盘读,从网络读,或者从键盘读,读到内存,就是InputStream。
不管你写入磁盘,写入网络,或者写到屏幕,都是OuputStream。
理解型:
有些人经常遇到InputStream、OuputStream,不知道哪是写出,哪个是写入,其实我们只要记住以内存为中心以及流的流向,就能很容易记住。
来个场景让你理解下:
如果我们往硬盘上写文件,用in还是out?从字面看,写文件啊,存储啊,必须是in啊,但是你没注意到数据流方向是从 内存---->硬盘,前面说了,以内存为中心,这是一种写出数据,对应的out,相反从硬盘读数据,数据流方向是从硬盘---->内存,所以得用in。
三.类构造解析:
FileInputStream和FileOutputStream作为文件流输入\输出对象,顾名思义,必然离不开File对象(不懂的参照上一篇)。
①FileInputStream
首先看一下 FileInputStream的构造函数:
FileInputStream(String name);
FileInputStream(File file);
FileInputStream(FileDescriptor fdObj);
从上面可以看出可以分别为File对象、字符串、FileDescriptor对象为参数构造FileInputStream对象。
然后看一下FileInputStream的主要方法:
read();
read(byte b[]);
read(byte b[], int off, int len);
close();
官方API解释:
1.read() : 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值。如果因为已经到达流末尾而没有可用的字节,则返回-1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
2.read(byte[] b) : 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。
将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到b[b.length-1] 的元素。
3.read(byte[] b,int off,int len):将输入流中最多 len 个数据字节读入字节数组。尝试读取多达 len 字节,但可能读取较少数量。以整数形式返回实际读取的字节数。在输入数据可用、检测到流的末尾或者抛出异常前,此方法一直阻塞。
如果
b
为
null
,则抛出
NullPointerException
。
off
为负,或
len
off+len
大于数组
b
的长度,则抛出
IndexOutOfBoundsException
len
为 0,则没有字节可读且返回
;否则,要尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值
-1
;否则,至少可以读取一个字节并将其存储在
b
中。
将读取的第一个字节存储在元素
b[off]
中,下一个存储在
b[off+1]
中,依次类推。读取的字节数最多等于
len
。让 k 为实际读取的字节数;这些字节将存储在元素
b[off]
至
b[off+
k
-1]
之间,其余元素
b[off+
]
至
b[off+len-1]
不受影响。
在任何情况下,元素
b[0]
b[off]
和元素
b[off+len]
b[b.length-1]
都不会受到影响。
如果不是因为流位于文件末尾而无法读取第一个字节,则抛出
IOException
。特别是,如果输入流已关闭,则抛出
IOException
类
InputStream
的
read(b,
off,
len)
方法只重复调用方法
read()
。如果第一个这样的调用导致
IOException
,则从对
read(b,
off,
len)
方法的调用中返回该异常。如果对
read()
的任何后续调用导致
IOException
,则该异常会被捕获并将发生异常时的位置视为文件的末尾;到达该点时读取的字节存储在
b
中并返回发生异常之前读取的字节数。建议让子类提供此方法的更有效的实现。
以上是3种read方法的详细介绍,下面我们来用代码解释一下:
㈠read()的用法
创建文件test.txt,里面我们只放入一个1,然后我们用FileinputStream读取。
public static void main(String[] args) {
try {
File file=new File("D:/test.txt");
FileInputStream input = new FileInputStream(file);
for(int i=1;i<=3;i++){
System.out.println(input.read());
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
--------------打印结果
49
-1
-1
我们来解读一下打印结果:
- read()方法是每调用一次,就从FileInputStream中读取一个字节。
- ANSI编码中,1只占用一个字节并且1的Ascii码为49。
-
返回下一个数据字节,如果已达到文件末尾,返回-1。
我们来画个图来解释下read读数据时的移动原理。
Java IO流(第三讲):字节流中的FileInputStream与FileoutputStream 注意:如果文件的编码方式不同,则上面打印结果会存在不同,因为不同的文件类型可能存在文件头
附:文本文件各格式文件头:ANSI类型:什么都没有,UTF-8类型:EF BB BF,UNICODE类型:FF FE,UNICODE BIG ENDIAN类型:FE FF
㈡read(byte[])的用法
创建文件test.txt,里面我们只放入1234567890,然后我们用FileinputStream读取。
public static void main(String[] args) {
try {
File file=new File("D:/test.txt");
byte[] b=new byte[3];
FileInputStream input = new FileInputStream(file);
for(int i=1;i<=5;i++){
System.out.println(input.read(b)+"---------"+Arrays.toString(b));
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
--------------打印结果
3---------[49, 50, 51]
3---------[52, 53, 54]
3---------[55, 56, 57]
1---------[48, 56, 57]
-1---------[48, 56, 57]
从打印结果进行分析:
- read(byte[])方法是每次将b.length个字节的数据读入一个byte数据组中。
- 返回读入缓冲的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回-1。
这里可能有一些同学会有疑问,第四次调用read(byte[])时,已经达到尾部,这时候应该是返回-1,这样不就导致最后一个数字读取不了吗,这个疑问让我们来看一下源码:
public int read(byte b[]) throws IOException {
Object traceContext = IoTrace.fileReadBegin(path);
int bytesRead = 0;
try {
bytesRead = readBytes(b, 0, b.length);//调用下面方法
} finally {
IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
}
return bytesRead;
}
-------------------------分割-----------------
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
<strong>int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}</strong>
return i;
}
从上代码可以看出,如果在某次读取时,指针开始不是指向文件末尾,则会去执行循环,如果执行循环的过程中指针到达末尾,则直接跳出循环,返回byte数组。
㈢read(byte[] b,int off,int len)的用法
见上面代码。
注意点:
因为read()每次执行都读取一个字节,中文经常在文件中占用两个字节,所以在调用read()方法的次数最好是偶数次,同理read(byte[])中数组的长度为偶数。
②FileOutputStream
FileOutputStream是从内存将数据读入文件的对象。
首先看一下构造函数
FileOutputStream(String name);
FileOutputStream(String name, boolean append);
FileOutputStream(File file);
FileOutputStream(File file, boolean append);
从源码可以看出所有构造函数最后都是FileOutputStream(File file, boolean append);这个构造函数,boolean append这个参数决定你是否将读出的内容追加到目标文件的末尾,没这个参数的构造器默认是false。
其次来看一看方法
write(int b);
write(byte b[]);
write(byte b[], int off, int len)
这三种方法主要是配合FileInputStream使用,这里我不做详细介绍了
附上实例:
public static void main(String[] args) {
try {
File file=new File("D:/test.txt");
File file1=new File("D:/test1.txt");
byte[] b=new byte[1024];
FileInputStream input = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(file1,true);
int i=0;
long startTime=System.currentTimeMillis();
while((i=input.read(b))>0){
out.write(b);
}
long endTime=System.currentTimeMillis();
System.out.println("共消耗"+(endTime-startTime));
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}