黑马程序员全套Java教程_Java基础教程_IO流之字节流(三十一)

2.1 IO流概述和分类

  • IO流概述:
    (1)IO:输出(Input)/输出(Output);
    (2)流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传输。
    (3)IO流就是用来处理设备间数据传输问题的,常见的应用有:文件复制、文件上传、文件下载。
  • 硬件层次的IO流:
    在这里插入图片描述
  • IO流分类:
    (1)按照数据的流向:输入流(读数据)、输出流(写数据);
    (2)按照数据类型:字节流(又分字节输入流和字节输出流)、字符流(又分字符输出流和字符输入流)。
    (3)一般来说,我们说IO流的分类是按照数据类型来分的。
    (4)字节流和字符流的使用场景:如果数据通过Windows自带的记事本软件打开,我们可以读懂里面的内容,就使用字符流,反之使用字节流。如果不知道该使用哪种类型的流,就使用字节流(因为字节流是万能的)。

2.2 字节流写数据

  • 字节流抽象基类:
    (1)InputStream:这个抽象类是表示字节输入流的所有类的超类;
    (2)OutputStream:这个抽象类是表示字节输出流的所有类的超类;
    (3)子类名特点:子类名称都是以其父类名作为子类名的后缀。
  • FileOutputStream:文件输出流用于将数据写入File。构造方法为FileOutputSream(String name),创建文件输出流,以指定的名称写入文件。
  • 使用字节输出流写数据的步骤:
    (1)创建字节输出流对象(做了三件事:调用系统功能创建了文件、创建字节输出流对象、让字节输出流对象指向文件);
    (2)调用字节输出流对象的写数据方法;
    (3)释放资源(关闭此文件输出流并释放与此相关联的任何系统资源)。
    public static void main(String[] args) throws IOException {
        //创建字节输出流对象
        /*
            做了三件事情:
            (1)调用系统功能创建了文件;
            (2)创建了字节输出流对象;
            (3)让字节输出流对象指向创建好的文件。
         */
        FileOutputStream fos = new FileOutputStream("fos.txt");

        //void write(int b):将指定的字节写入文件输出流
        fos.write(97);
        fos.write(57);
        fos.write(55);

        //最后要释放资源
        //void close():关闭文件输出流并释放与此相关联的任何系统资源
        fos.close();
    }

2.3 字节流写数据的三种方式

方法名说明
void write(int b)将指定的字节写入此文件输出流,一次写入一个字节数据
void write(byte[] b)将b.length字节从指定的字节数组写入此文件输出流
void write(byte[] b, int off, int len)将len字节从指定的字节数组开始,从偏移量off写入此文件输出流,一次写一个字节数组的部分数据
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt");

        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.write(100);

        //byte[] bys = {97, 98, 99, 100};

        byte[] bys = "abcde".getBytes();
        fos.write(bys);

        fos.write(bys, 0, bys.length);
        fos.write(bys, 1, 3);

		fos.close();
    }

2.4 字节流写数据的两个小问题

  • 字节流要想实现换行,在写完数据后,加换行符即可:
    (1)Windows:\r\n
    (2)Linux:\n
    (3)mac:\r
  • 字节流写数据想要实现追加写入:public FileOutputStream(String name; boolean append)。创建文件输出流以指定的名称写入文件,如果第二个参数为true,则字节将写入文件的末尾而不是开头。
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt",true);

        for (int i = 0; i<10;i++){
            fos.write("hello".getBytes());
            fos.write("\r\n".getBytes());
        }

        fos.close();
    }

2.5 字节流写数据加异常处理

  • finally
    (1)在异常处理时提供finally块来执行所有清除操作,比如IO流中的释放资源;
    (2)特点:被finally控制的语句一定会执行,除非JVM退出。
try{
	可能出现异常的代码;
}catch{
	异常的处理代码;
}finally{
	执行所有清除操作;
}
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //fos = new FileOutputStream("Z:\\fos.txt");//会导致两个错误,错误1:FileNotFoundException系统找不到指定的路径
            fos = new FileOutputStream("fos.txt");
            fos.write("hello".getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();//错误2:如果fos指定的路径没有找到,会报NullPointerException(因为我们一开始给它定义的是null)。所以我们这里最好加一个判断
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

面试题:执行example()方法,不会被输出到控制台的是()

    static void procedure() {
        try {
            int c[] = {1};
            c[42] = 99;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Test 4");
        }
    }
    public static void example(){
        try {
            procedure();
            System.out.println("Test 1");
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("Test 2");
        }finally {
            System.out.println("Test 3");
        }
    }
  • Test 2。在procedure()中已经catch到ArrayIndexOutOfBoundsException异常并且将其解决了,轮不到example()来catch异常了。

2.6 字节流读数据

  • FileInputStream:从文件系统中的文件获取输入字节。构造方法为FileInputSream(String name),该文件由文件系统中的路径名name命名。

  • 使用字节输出流读数据的步骤:
    (1)创建字节输入流对象;
    (2)调用字节输入流对象的读数据方法;
    (3)释放资源。

  • 需求:把fos.txt中的内容读取出来输出在控制台。

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");

        //int read = fis.read();
        //System.out.println(read1);
        //System.out.println((char) read);
        //read = fis.read();
        //System.out.println((char) read);
        //......

        //优化1:
        //int read = fis.read();
        //while (read!=-1){
        //    System.out.print((char) read);
        //    read = fis.read();
        //}

        //优化2(标准写法):
        int read;
        /*
            1、fis.read()
            2、read =fis.read()
            3、read!=-1
         */
        while ((read =fis.read())!=-1){
            System.out.print((char)read);
        }

        fis.close();
    }

案例:复制文本文件

  • 需求:把“E:\itcast\窗里窗.txt”的内容复制到模块目录下的“窗里窗外.txt”内。
  • 分析:
    (1)创建文本文件,其实就是把文本文件内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地);
    (2)数据源:E:\itcast\窗里窗外.txt -> 读数据 -> InputSream -> FileInputStream;
    (3)目的地:窗里窗外.txt -> 写数据 -> OutputStream -> FileOutputStream。
  • 思路:
    (1)根据数据源创建字节输入流对象;
    (2)根据目的地创建字节输出流对象;
    (3)读写数据,复制文本文件(一次读取一个字节,一次写入一个字节);
    (4)释放资源。
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\itcast\\窗里窗外.txt");
        FileOutputStream fos = new FileOutputStream("窗里窗外.txt",true);

        int by;
        while ((by = fis.read()) != -1){
            fos.write(by);
        }

        fis.close();
        fos.close();
    }

2.6 字节流读数据(一次读一个字节数组)

  • 使用字节输入流读数据的步骤:
    (1)创建字节输入流对象;
    (2)调用字节输入流对象的读数据方法;
    (3)释放资源。
  • 需求:把文件fos.txt中的内容读取出来在控制台输出。
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fis.txt");

        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) != -1){///fis.read(bys)返回读取到的最大数据个数,受数组大小和数据个数影响
            System.out.println(new String(bys,0,len));//new String(byte[] byte):将字节数组转换为字符串
        }

        fis.close();
    }

案例:复制图片

  • 需求:把“E:\itcast\mn.jpg”复制到模块目录下的“mn.jpg”。
  • 思路:
    (1)根据数据源创建字节输入流对象;
    (2)根据目的地创建字节输出流对象;
    (3)读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组);
    (4)释放资源。
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\itcast\\mn.jpg");
        FileOutputStream fos  = new FileOutputStream("mn.jpg");

        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) != -1){
            fos.write(bys,0,len);
        }

        fis.close();
        fos.close();
    }

2.7 字节缓冲流

  • 字节缓冲流:
    (1)BufferOutputStream:该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为每个的字节导致底层系统的调用。
    (2)BufferInputStream:创建BufferInputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时,内部缓冲区将根据需要从包含的输入流中重新填充,一次很多个字节。
  • 构造方法:
    (1)字节缓冲输出流:BufferedOutputStream(OutputStream out);
    (2)字节缓冲输入流:BufferedInputStream(InputStream in);
    (3)构造放法传入的是字节流,而不是具体的文件/路径的原因:字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作。
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("窗里窗外.txt"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("窗里窗外.txt"));

        bos.write("dqeiqheieqheiq".getBytes());
        bos.write("\nedgnb".getBytes());
        bos.close();

        //int by;
        //while ((by = bis.read()) != -1){
        //    System.out.print((char)by);
        //}

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1){
            System.out.println(new String(bys,0,len));
        }

        bis.close();
    }

案例:复制视频

  • 需求:把E:\itcast\vlog.avi”复制到模块目录下的“vlog.avi”。
  • 思路:
    (1)根据数据源创建字节输入流对象;
    (2)根据目的地创建字节输出流对象;
    (3)读写数据,复制视频;
    (4)释放资源。
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\itcast\\vlog.avi");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("vlog.avi");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        long startTime = System.currentTimeMillis();

        //方法1(字节缓冲流一次读写一个字节):使用了:10秒
        //int by;
        //while ((by = bis.read()) != -1){
        //    bos.write(by);
        //}

        //方法2(字节缓冲流一次读写一个数组):使用了:0秒
        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1){
            bos.write(bys);
        }

        //方法3(基本字节流一次读写一个字节):等得不想等了,毁灭吧!
        //int by;
        //while ((by = fis.read()) != -1){
        //    fos.write(by);
        //}

        //方法4(基本字节流一次读写一个数组):使用了:2秒
        //byte[] bys = new byte[1024];
        //int len;
        //while ((len = fis.read(bys)) != -1){
        //    fos.write(bys);
        //}

        bis.close();
        bos.close();
        fis.close();
        fos.close();

        long endTime = System.currentTimeMillis();
        System.out.println("使用了:" + ((endTime-startTime)/1000) + "秒");
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值