io流 字节流字符流

使用情况:如果数据通过Window自带的记事本软件打开,我们还可以读懂里面的内容,就使用字符流;否则使用字节流。如果不知道该使用那种类型的流,就使用字节流(万能流)。

FileWriter类(重点)

主要用于将文本内容写入到文本文件。

FileReader类(重点)

主要用于从文本文件读取文本数据内容。

FileOutputStream类(重点)

java.io.FileOutputStream类主要用于将图像数据之类的原始字节流写入到输出流中。

FileInputStream类(重点)

主要用于从输入流中以字节流的方式读取图像数据等。

FileInputStream类(重点)

主要用于从输入流中以字节流的方式读取图像数据等。

BufferedOutputStream类(重点)

主要用于描述缓冲输出流,此时不用为写入的每个字节调用底层 系统。

BufferedInputStream类(重点)

主要用于描述缓冲输入流。

BufferedWriter类(重点)

主要用于写入单个字符、字符数组以及字符串到输出流中。

BufferedReader类(重点)

用于从输入流中读取单个字符、字符数组以及字符串。

public class BufferedCharCopyTest {

    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            // 1.创建BufferedReader类型的对象与d:/a.txt文件关联
            br = new BufferedReader(new FileReader("d:/a.txt"));
            // 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
            bw = new BufferedWriter(new FileWriter("d:/b.txt"));
            // 3.不断地从输入流中读取一行字符串并写入到输出流中
            System.out.println("正在玩命地拷贝...");
            String str = null;
            while ((str = br.readLine()) != null) {
                bw.write(str);
                bw.newLine(); // 当前系统中的行分隔符是:\r\n
            }
            System.out.println("拷贝文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭流对象并释放有关的资源
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class BufferedByteCopyTest {

    public static void main(String[] args) {

        // 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
        long g1 = System.currentTimeMillis();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            // 1.创建BufferedInputStream类型的对象与d:/02_IO流的框架结构.mp4文件关联
            bis = new BufferedInputStream(new FileInputStream("d:/02_IO流的框架结构.mp4"));
            // 2.创建BufferedOuputStream类型的对象与d:/IO流的框架结构.mp4文件关联
            bos = new BufferedOutputStream(new FileOutputStream("d:/IO流的框架结构.mp4"));

            // 3.不断地从输入流中读取数据并写入到输出流中
            System.out.println("正在玩命地拷贝...");

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

            System.out.println("拷贝文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭流对象并释放有关的资源
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long g2 = System.currentTimeMillis();
        System.out.println("使用缓冲区拷贝视频文件消耗的时间为:" + (g2-g1)); // 44
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值