定义小数组
- write(byte[] b)
- write(byte[] b, int off, int len):写出有效的字节个数
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo07_ArrayCopy {
/**
* @param 第三种拷贝/定义小数组
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//demo01();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
byte[] arr = new byte[2];
int len;
//如果忘记加arr,返回的就不是读取的字节个数,而是字节的码表值
while((len = fis.read(arr)) != -1) {
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
public static void demo01() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr); //将文件上的字节读取到字节数组中
System.out.println(a); //读到的有效字节个数
for (byte b : arr) { //第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("-----------------------");
int c = fis.read(arr);
System.out.println(c);
for (byte b : arr) {
System.out.println(b);
}
fis.close();
}
}
demo01的原理图
第一次读取:97,98
第二次读取:99把97的位置覆盖了,但是98没有字节数把他覆盖,所以就是99,97