栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Java零基础day20

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java零基础day20

文章目录
  • 一、缓冲的概念
  • 二、IO流的分类
    • 1.字节输入输出流
    • 2.字符输入输出流
    • 综合案例


一、缓冲的概念

看视频有点卡?暂停一下,加载缓冲一下。

快递:送到物流中转站,然后分批次的发。物流中转站就是缓冲的概念。

IO流的本质就是对电脑的文件进行读和写的

计算机通过CPU内存读取磁盘上面的文件数据,一次读取1字节。但是可以加上缓冲的概念

每次读取4kb,效率会变高

二、IO流的分类 1.字节输入输出流
package com.qfedu.fileoutstream;

import java.io.*;


public class Demo2 {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(("F:/aaa/1.mp4")));

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(("F:/aaa/bbb/1.mp4")));
        byte[] bytes = new byte[4 * 1024];
        int length;
        while ((length = bis.read(bytes)) != -1) {
            System.out.println("哈哈");
            bos.write(bytes, 0, length);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - start);//324ms
        copyVideo();

    }

    public static void copyVideo() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(("F:/aaa/1.mp4"));
        FileOutputStream fos = new FileOutputStream(("F:/aaa/bbb/1.mp4"));
        int length;
        while ((length = fis.read()) != -1) {
            System.out.println("哈哈");
            fos.write(length);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - start);//444435ms
    }
}
2.字符输入输出流

FileReader:是一个阅读字符文件的便利类,是专门处理字符文件的,比如txt文件。

是从字节流到字符流的桥:它读取字节,并使用指定的charset将其解码为字符,它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集

牵涉到解码,底层是字节流,但是会解码为字符。如果解码失败就意味着咱们读取失败了

一般不会使用字符流操作图片,音频,视频等,因为牵涉到解码。会出问题!!!开发一般使用字节流!!!

package com.qfedu.task;

import java.io.*;


public class Demo4 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(("F:/aaa/斗破苍穹.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(("F:/aaa/ccc/斗破苍穹.txt")));
        char[] chs = new char[4 * 1024];
        int length;
        while ((length = br.read(chs)) != -1) {
            bw.write(chs, 0, length);
        }
        bw.close();
        br.close();
    }
}
综合案例
package com.qfedu.task;

import java.io.*;
import java.util.Scanner;


public class Demo5 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择使用方法1.字节IO流 2.字符IO流");
        int num = scanner.nextInt();
        long start = System.currentTimeMillis();
        if (num == 1) {
            txtOrMp3OrMp4Copy();
        } else if (num == 2) {
            txtCopy();
        } else {
            System.out.println("输入错误!!!");
        }
        long end = System.currentTimeMillis();
        System.out.println("复制成功,共用时" + (end - start) / 1000 + "秒");
    }

    //字节IO流
    public static void txtOrMp3OrMp4Copy() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要复制的文件存放绝对路径");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream((scanner.next())));
        System.out.println("请输入复制后的文件存放绝对路径");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream((scanner.next())));
        byte[] bytes = new byte[4 * 1024];
        int length;
        while ((length = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, length);
        }
        bos.close();
        bis.close();
    }

    //字符IO流
    public static void txtCopy() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要复制的文件存放绝对路径");
        BufferedReader br = new BufferedReader(new FileReader((scanner.next())));
        System.out.println("请输入复制后的文件存放绝对路径");
        BufferedWriter bw = new BufferedWriter(new FileWriter((scanner.next())));
        char[] chs = new char[4 * 1024];
        int length;
        while ((length = br.read(chs)) != -1) {
            bw.write(chs, 0, length);
        }
        bw.close();
        br.close();
    }
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039845.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号