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("\r\n".getBytes());
        }
        // 追加写入 实例化FileOutputStream的时候 构造方法 选择下面这个带true的
        fos = new FileOutputStream("a.txt", true);
        //
        // 换行写入
        for (int i = 0; i < 10; i++) {
            fos.write("刘德华".getBytes());
            fos.write("\r\n".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("银鞍照白马\r\n".getBytes());
        bos.write("飒沓如流星\r\n".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();

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

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

猜你喜欢

转载自www.cnblogs.com/Alex-zqzy/p/9121702.html