1. IO流的分类:
流向:输入、输出流
数据类型:字节流、字符流
2. 字节流:FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream(后两个为高效字节流,自带缓冲区)
3. 为什么字节高效缓冲流的构造方法的参数是一个抽象类而不是具体的文件或者文件路径?
因为字符缓冲区流仅仅提供缓冲区,为高效而设计,但是真正的读写操作还是靠基本的流对象来实现
4. 字节流复制文件的四种方式
第一种方法:一次读取一个字节,并写入
//封装数据源
FileInputStream fis = new FileInputStream("D:\\KuGou\\高进、小沈阳 - 我的好兄弟 - 原版伴奏.mp3");
//封装目的地
FileOutputStream fos = new FileOutputStream("h.mp3");
int by = 0;
//一次读取一个字节,如果读到文件末尾,则返回-1;
while ((by = fis.read()) != -1) {
fos.write( by);
//一次写入一个字节
}
//释放资源
fis.close();
fos.close();
第二种方法:一次读取一个字节数组
//封装数据源
FileInputStream fis = new FileInputStream("D:\\KuGou\\高进、小沈阳 - 我的好兄弟 - 原版伴奏.mp3");
//封装目的地
FileOutputStream fos = new FileOutputStream("i.mp3");
//定义一个字节数组,数组大小一般为1024或者1024的整数倍,即以k为单位
byte[] by = new byte[1024];
int length = 0;
while ((length = fis.read(by)) != -1) {
//写入实际读到的数组长度
fos.write(by, 0, length);
}
fis.close();
fos.close();
第三种方法:高效字节流:一次读取一个字节
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\KuGou\\高进、小沈阳 - 我的好兄弟 - 原版伴奏.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("j.mp3"));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bis.close();
bos.close();
第四种方法:高效字节流,一次读取一个字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\KuGou\\高进、小沈阳 - 我的好兄弟 - 原版伴奏.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("k.mp3"));
byte[] by = new byte[1024];
int length = 0;
while ((length = bis.read(by)) != -1) {
bos.write(by, 0, length);
}
bis.close();
bos.close();