Java学习-工具IDEA-黑马视频(第十六天)

FileOutputStream

基本写数据

 		//创建字节流对象
//        //创建文件输出流以指定得名称写入文件
//        FileOutputStream fos = new FileOutputStream("idea_test\\src\\com\\FIle\\fos.txt");

        //为true时表示追加写入
        FileOutputStream fos = new FileOutputStream("idea_test\\src\\com\\FIle\\fos.txt",true);

        //将指定的字节写入此文件输出流,ASCII码
        fos.write(97);//a

        //将指定的字节数组写入此文件输出流,ASCII码
        byte[] bys = {23,43,23,53,32,54};
        fos.write(bys);
        /**
         * 换行符识别:
         * Win:\r\n
         * linux:\n
         * mac:\r
         */

        fos.write("\n".getBytes());
        //返回字符串对应字节数组
        byte[] bys2 = "JYQWBD".getBytes();
        //从字节数组指定起点off开始写len长度的字节到数据流
        fos.write(bys2,1,3);
        //关闭此文件输出流并释放与此流相关联的任何系统资源
        fos.close();

异常处理

        /**
         * finally:在异常处理时提供finally块来执行所有的清楚操作。比如IO流中的释放资源
         * 特点:被finally控制的语句一定会执行,除非JVM退出
         */
        FileOutputStream ofs = null;
        try {
            ofs = new FileOutputStream("idea_test\\\\src\\\\com\\\\FIle\\\\fos.txt");
            ofs.write("hello".getBytes());
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            //加if的原因是因为若前面给ofs赋值失败时,ofs依然为空,如果不判空则下面的close操作会报错
            if(ofs != null){
                try {
                    //如果不这样弄,当出现异常时程序会一直占用该资源
                    ofs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

FileInputStream

读取单个字节

        FileInputStream fis = new FileInputStream("idea_test\\\\src\\\\com\\\\FIle\\\\fos.txt");

        /**
         * 调用字节输入流对象的读数据方法
         * read():从该输入流读取一个字节的数据
         * 如果读取到文件末尾则返回-1
         */
        int by = fis.read();
        while(by!=-1){
            System.out.print((char) by);
            by = fis.read();
        }
        //优化
        int by2 = 0;
        while((by2=fis.read())!=-1){
            System.out.println((char)by2);
        }

        //释放资源
        fis.close();

读取一个字节数组的数据

		FileInputStream fis = new FileInputStream("idea_test\\src\\com\\FIle\\fos.txt");
//        byte[] bys = new byte[5];
//        int len = fis.read(bys);
//        System.out.println(len);
//        System.out.println(new String(bys));
//        /**
//         * hello\n
//         * world\n
//         * 第一次:hello
//         * 第二次:\nworl
//         * 第三次:d\n
//         */
//        /**
//         * 最后结果
//         * 5
//         * hello
//         * 5
//         *
//         * worl
//         * 1
//         * dworl
//         * 最后一次为dworl原因是bys中上一次已经给字节数组赋值了,而第三次操作只修改了第一个字节
//         * 所以为了避免读取上次留下的字节,需要将其修改为new String(bys,0,len)
//         */
//        len = fis.read(bys);
//        System.out.println(len);
//        System.out.println(new String(bys));
//
//        len = fis.read(bys);
//        System.out.println(len);
//        System.out.println(new String(bys));

        //升级
        byte[] bys2 = new byte[1024];//一般给1024及其整数倍
        int len2 = 0;
        while((len2 = fis.read(bys2))!=-1){
            System.out.println(new String(bys2,0,len2));
        }

复制图片

        FileInputStream fis = new FileInputStream("C:\\Users\\17822\\Desktop\\个人资料\\毕业.jpg");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\17822\\Desktop\\毕业2.jpg");

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

复制视频

 //记录开始时间
        long startTime = System.currentTimeMillis();

//        method1();//6520ms
//        method2();//27ms
//        method3();//45ms
        method4();//13ms
        //记录结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - startTime) + "ms");
    }

    //基本字节流一次读写一个字节
    public static void method1() throws IOException {
        FileInputStream fis = new FileInputStream("idea_test\\src\\com\\FIle\\狗.mp4");
        FileOutputStream fos = new FileOutputStream("idea_test\\src\\com\\FIle\\狗2.mp4");

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

    //基本字节流一次读写一个字节数组
    public static void method2() throws IOException {
        FileInputStream fis = new FileInputStream("idea_test\\src\\com\\FIle\\狗.mp4");
        FileOutputStream fos = new FileOutputStream("idea_test\\src\\com\\FIle\\狗2.mp4");

        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 method3() throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("idea_test\\src\\com\\FIle\\狗.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("idea_test\\src\\com\\FIle\\狗2.mp4"));
        int by =0;
        while((by = bis.read())!=-1){
            bos.write(by);
        }
        bos.close();
        bis.close();
    }

    //字节缓冲流一次读写一个字节数组
    public static void method4() throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("idea_test\\src\\com\\FIle\\狗.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("idea_test\\src\\com\\FIle\\狗2.mp4"));

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值