Io流之字节流

IO流(掌握)

    (1)IO用于在设备间进行数据传输的操作   

    (2)分类:

        A:流向

            输入流    读取数据

            输出流    写出数据

        B:数据类型

            字节流   

                    字节输入流     读取数据    InputStream

                    字节输出流     写出数据    OutputStream

            字符流

                    字符输入流     读取数据    Reader

                    字符输出流     写出数据    Writer

        注意:

            a:如果我们没有明确说明按照什么分,默认按照数据类型分。

            b:除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,否则建议使用字节流。

            c:注意:抽象类不能实例化,所以,我们要找一个具体的子类。每种基类的子类都是以父类名作为后缀名。XxxOutputStream  

                         XxxInputStream  XxxReader XxxWriter

    (3)FileOutputStream写出数据

           构造方法:

               FileOutputStream(File file)

               FileOutputStream(String name) 

            方法:

               public void write(int b):写一个字节

               public void write(byte[] b):写一个字节数组

               public void write(byte[] b,int off,int len):写一个字节数组的一部分

        A:操作步骤

            a:创建字节输出流对象

            b:写数据:调用write()方法

            c:释放资源

 

        B:代码体现:

            FileOutputStream fos = new FileOutputStream("fos.txt");

            fos.write("hello".getBytes());

            //fos.write(97); //97 -- 底层二进制数据    -- 通过记事本打开 -- 找97对应的字符值 -- a

            fos.close();

 

        C:要注意的问题?

            a:创建字节输出流对象做了几件事情?

                  A:调用系统功能去创建文件

                  B:创建fos(File文件)对象

               C:把fos对象指向这个文件

            b:为什么要close()?

                   A:让流对象变成垃圾,这样就可以被垃圾回收器回收了

                   B:通知系统去释放跟该文件相关的资源

            c:如何实现数据的换行?

                    windows:\r\n

                    linux:\n

                    Mac:\r

                    而一些常见的个高级记事本,是可以识别任意换行符号的。

            d:如何实现数据的追加写入?

                    用构造方法带第二个参数是true的情况即可

                 

 // 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。

        FileOutputStream fos = new FileOutputStream("fos3.txt", true);



        // 写数据

        for (int x = 0; x < 10; x++) {

            fos.write(("hello" + x).getBytes());

            fos.write("\r\n".getBytes());

        }

        // 释放资源

        fos.close();

 

    (4)FileInputStream读取数据

          读取数据的方式:

               A:int read():一次读取一个字节

               B:int read(byte[] b):一次读取一个字节数组

        A:操作步骤

            a:创建字节输入流对象

            b:调用read()方法读取数据 

            c:释放资源

 

        B:代码体现:         

 

            FileInputStream fis = new FileInputStream("fos.txt");



            //方式1

            int by = 0;

            // 读取,赋值,判断

            while((by=fis.read())!=-1) {

                System.out.print((char)by);

            }



            //方式2

            // 数组的长度一般是1024或者1024的整数倍

            byte[] bys = new byte[1024];

            int len = 0;

            while((len=fis.read(bys))!=-1) {

                System.out.print(new String(bys,0,len));

            }



            fis.close();

    (5)案例:2种方式

        A:复制文本文件

        B:复制图片

        C:复制视频


/*

 * 复制文本文件。

 *

 * 数据源:从哪里来

 * a.txt    --    读取数据    --    FileInputStream   

 *

 * 目的地:到哪里去

 * b.txt    --    写数据        --    FileOutputStream

 *

 * java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)

 *

 * 这一次复制中文没有出现任何问题:通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。

 * 它会自己做转换。

 */

        // 封装数据源

        FileInputStream fis = new FileInputStream("a.txt");

        // 封装目的地

        FileOutputStream fos = new FileOutputStream("b.txt");



        int by = 0;

        while ((by = fis.read()) != -1) {

            fos.write(by);

        }



        // 释放资源

        fos.close();

        fis.close();

/*

 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中

 *

 * 数据源:

 *         e:\\哥有老婆.mp4--读取数据--FileInputStream

 * 目的地:

 *         copy.mp4--写出数据--FileOutputStream

 */

        // 封装数据源

        FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");

        // 封装目的地

        FileOutputStream fos = new FileOutputStream("copy.mp4");



        // 复制数据

        byte[] bys = new byte[1024];

        int len = 0;

        while ((len = fis.read(bys)) != -1) {

            fos.write(bys, 0, len);

        }



        // 释放资源

        fos.close();

        fis.close();

 

    (6)字节缓冲区流

        A:BufferedOutputStream     

     

        BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("bos.txt"));

        // 写数据

        bos.write("hello".getBytes());

        // 释放资源

        bos.close();

        B:BufferedInputStream


         BufferedInputStream bis = new BufferedInputStream(new FileInputStream( "bos.txt"));

         byte[] bys = new byte[1024];

         int len = 0;

         while ((len = bis.read(bys)) != -1) {

             System.out.print(new String(bys, 0, len));

         }

         // 释放资源

         bis.close();

 

    (7)案例:


/*

 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中

 *

 * 字节流四种方式复制文件:

 * 基本字节流一次读写一个字节:    共耗时:117235毫秒

 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒

 * 高效字节流一次读写一个字节: 共耗时:1141毫秒

 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒

 */

public class CopyMp4Demo {

    public static void main(String[] args) throws IOException {

        long start = System.currentTimeMillis();

        // method1("e:\\哥有老婆.mp4", "copy1.mp4");

        // method2("e:\\哥有老婆.mp4", "copy2.mp4");

        // method3("e:\\哥有老婆.mp4", "copy3.mp4");

        method4("e:\\哥有老婆.mp4", "copy4.mp4");

        long end = System.currentTimeMillis();

        System.out.println("共耗时:" + (end - start) + "毫秒");

    }



    // 高效字节流一次读写一个字节数组:

    public static void method4(String srcString, String destString)

            throws IOException {

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(

                srcString));

        BufferedOutputStream bos = new BufferedOutputStream(

                new FileOutputStream(destString));



        byte[] bys = new byte[1024];

        int len = 0;

        while ((len = bis.read(bys)) != -1) {

            bos.write(bys, 0, len);

        }



        bos.close();

        bis.close();

    }



    // 高效字节流一次读写一个字节:

    public static void method3(String srcString, String destString)

            throws IOException {

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(

                srcString));

        BufferedOutputStream bos = new BufferedOutputStream(

                new FileOutputStream(destString));



        int by = 0;

        while ((by = bis.read()) != -1) {

            bos.write(by);



        }



        bos.close();

        bis.close();

    }



    // 基本字节流一次读写一个字节数组

    public static void method2(String srcString, String destString)

            throws IOException {

        FileInputStream fis = new FileInputStream(srcString);

        FileOutputStream fos = new FileOutputStream(destString);



        byte[] bys = new byte[1024];

        int len = 0;

        while ((len = fis.read(bys)) != -1) {

            fos.write(bys, 0, len);

        }



        fos.close();

        fis.close();

    }



    // 基本字节流一次读写一个字节

    public static void method1(String srcString, String destString)

            throws IOException {

        FileInputStream fis = new FileInputStream(srcString);

        FileOutputStream fos = new FileOutputStream(destString);



        int by = 0;

        while ((by = fis.read()) != -1) {

            fos.write(by);

        }



        fos.close();

        fis.close();

    }

}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值