1.分类:
-
按照数据的流向
输入流:读数据
输出流:写数据
-
按照数据类型来分
字节流
字节输入流;字节输出流
字符流
字符输入流;字符输出流
-
IO流分类一般按照数据类型来分
-
打开记事本,能读懂里面的内容,就使用字符流
否则使用字节流,如果不确定,那就字节流,这个是万能流。
-
硬盘-------->内存 :输入流-读数据 ..... 读到内存
-
硬盘<--------内存 :输出流-写数据 ......写到硬盘
-
字节流抽象基类
-
inputStream: 这个抽象类是表示字节输入流的所有类的超类
-
outputStream:这个抽象类是表示字节输出流的所有类的超类
-
子类特点:子类名称都是以其父类名字作为类名的后缀。
-
*字节流
1.字节缓冲流
-
BufferOutputStream:
-
BufferInputStream:
-
字节缓冲流 仅仅提供缓冲区
2.字节流:输入输出流和缓冲流
package com.yang.io;
import java.io.*;
//练习
public class Demo12 {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
// method1();//共耗时408298时间
// method2();//共耗时516时间
// method3();//共耗时407时间
method4();//共耗时94时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时"+(endTime-startTime)+"时间");
}
//字节缓冲流一次读取一个字节数组
public static void method4() throws IOException{
//缓冲流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Study\\files\\javaSe\\hh.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\Study\\files\\gg.avi"));
//读数据
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys)) != -1){
//写数据
bos.write(bys,0,len);
}
//关闭流
bis.close();
bos.close();
}
//字节缓冲流一次读取一个字节
public static void method3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Study\\files\\javaSe\\hh.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\Study\\files\\gg.avi"));
int by;
while ((by=bis.read())!=-1){
bos.write(by);
}
bis.close();
bos.close();
}
//一次读写一个数组
public static void method2() throws IOException{
FileInputStream fis = new FileInputStream("D:\\Study\\files\\javaSe\\hh.avi");
FileOutputStream fos = new FileOutputStream("D:\\Study\\files\\gg.avi");
byte[] bys = new byte[1024];
int len;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}