java IO流复制文本,视频文件

  1. java IO流的分类:按照流向可分为输入流和输出流,按照数据类型可分为字节流和字符流。
  2. IO流的适用场景和效率:在处理音频,视频,图片这些文件时一般使用字节流,而使用缓冲字节流(BufferedInputStream和BufferedOutputStream)会比文件字节流(FileInputStream和FileOutputStream)的效率高;在处理文本文件时一般使用字符流,而使用缓冲字符流(BufferedReader和BufferedWriter)会比文件字符流(FileReader和FileWriter)的效率高。
  3. 下面用一张图来概括IO流体系:
    这里写图片描述

  4. 用字节流复制视频并比较效率

package com.yixun.cn;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class CopyPicture {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv"));
            fos = new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv"));
            byte[] b = new byte[64];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("文件字节流复制视频花费时间为:" + (endTime - startTime));
    }

    @Test
    public void test() {
        long startTime = System.currentTimeMillis();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv")));
            bos = new BufferedOutputStream(
                    new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv")));
            byte[] b = new byte[64];
            int len = 0;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.flush();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲字节流复制视频花费时间为:" + (endTime - startTime));
    }
}
 5. 用字符流复制文本文件并比较效率
package com.yixun.cn;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.junit.Test;

public class CopyText {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(new File(
                    "C:\\Users\\mayu\\Desktop\\xxx.txt"));
            fw = new FileWriter(new File("C:\\Users\\mayu\\Desktop\\test\\xxx.txt"));
            char[] c = new char[64];
            int len = 0;
            while ((len = fr.read(c)) != -1) {
                fw.write(c, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.flush();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("文件字符流复制文本花费时间为:" + (endTime - startTime));
    }

    @Test
    public void test() {
        long startTime = System.currentTimeMillis();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
                    "C:\\\\Users\\\\mayu\\\\Desktop\\\\xxx.txt"))));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    new File("C:\\Users\\mayu\\Desktop\\test\\x"))));
            char[] c = new char[64];
            int len = 0;
            while ((len = br.read(c)) != -1) {
                bw.write(c, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.flush();
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲字符流复制文本花费时间为:" + (endTime - startTime));
    }
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值