zoukankan      html  css  js  c++  java
  • java基础之io流总结三:字节流读写

    字节流读写适用于任何文件,包括图片,视频等。

    基本字节流

    一次读一个字节和一次读一个字节数组

            FileInputStream fis = new FileInputStream(path);
            //一次读一个字节
            int by;
            //read()==-1则代表文件读到了末尾
            while ((by = fis.read()) != -1) {
                System.out.print((char) by);
            }
    
            //一次读一个字节数组 并转换成字符串
            byte[] bytes = new byte[1024];
            int len;
            //read()==-1则代表文件读到了末尾
            while ((len = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
                len = fis.read(bytes);
            }
            //close
            fis.close();

    一次写一个字节和一次写一个字节数组

            FileOutputStream fos = new FileOutputStream(path);
            // 写入方法
            fos.write(65);
            byte[] bytes = { 1, 2, 3, 4, 5 };
            fos.write(bytes);
            // 写入字符串
            byte[] bys = "字节流输出写入文件2".getBytes();
            fos.write(bys);
            // 写字符串一部分
            fos.write(bys, 0, 4);
    
            // 换行写入
            for (int i = 0; i < 10; i++) {
                fos.write("张学友".getBytes());
                fos.write("
    ".getBytes());
            }
            // 追加写入 实例化FileOutputStream的时候 构造方法 选择下面这个带true的
            fos = new FileOutputStream("a.txt", true);
            //
            // 换行写入
            for (int i = 0; i < 10; i++) {
                fos.write("刘德华".getBytes());
                fos.write("
    ".getBytes());
            }
    
            // close
            fos.close();

    复制文件:也就是读出来直接写入到新文件中

            // 首先读
            FileInputStream fis = new FileInputStream(inputPath);
            // 然后写
            FileOutputStream fos = new FileOutputStream(outPutPath);
            int by;//一次读写一个字节
            while ((by = fis.read()) != -1) {
                fos.write(by);
            }
    
            // 一次读写一个字节数组
            byte[] bys = new byte[1024];
            int len;
            while ((len = fis.read(bys)) != -1) {
                fos.write(bys, 0, len);
            }
            fis.close();
            fos.close();

     

    缓冲区字节流

    一次读一个字节和一次读一个字节数组

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            // 单字节
            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();

    一次写一个字节和一次写一个字节数组

            // 写入  new FileOutputStream(path, true) 第二个参数设置为true代表是在文件中追加写入
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path, true));
            bos.write(64);
            bos.write("银鞍照白马
    ".getBytes());
            bos.write("飒沓如流星
    ".getBytes());
            bos.close();

    复制文件:也就是读出来直接写入到新文件中

            // 缓冲区字节流一次读写一个字节
            BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream(outPutPath));
            BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(inputPath));
            int by2;
            while ((by2 = bis1.read()) != -1) {
                bos1.write(by2);
            }
            bos1.close();
            bis1.close();// 缓冲区字节流一次读写一个字节数组
            BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(inputPath));
            BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream(outPutPath));
            byte[] bys4 = new byte[1024];
            int len4;
            while ((len4 = bis2.read(bys4)) != -1) {
                bos2.write(bys4, 0, len4);
            }
            bos2.close();
            bis2.close();

    一次读写一个字节数组要比一次读写一个字节的速度快

    缓冲区字节流一次读写一个字节数组比基本字节流一次读写一个字节数组速度快

  • 相关阅读:
    【转】【Linux】linux awk命令详解
    【转】【Linux】Linux 命令行快捷键
    【转】【Linux】sed命令详解
    【转】【Linux】Linux 下zip包的压缩与解压
    【转】【Linux】grep命令详解
    【转】crontab命令 脚本定时运行
    【转】BAT 批处理脚本 教程
    【MySql】脚本备份数据库
    php的json校验json-schema
    phan—php语法静态检查在windows下的配置
  • 原文地址:https://www.cnblogs.com/blazeZzz/p/9121702.html
Copyright © 2011-2022 走看看