Java的IO流复制文件

使用Java的IO分为字节流的IO和字符流的IO,需要说明的是:

1.复制文本文件(也就是可以通过记事本打开查看的),可以使用字符流也可以使用字节流,推荐使用字符流;复制其他文件(不能用记事本打开查看的)只能使用字节流.

2.字节流和字符流的IO都有两种方式,普通的IO和缓冲区IO,缓冲区IO快于普通IO,推荐使用缓冲区IO.

3.以下案例中,前两个为字节流案例,后四个为字符流案例,字符流复制非常推荐使用copy6的方法

package cn.lg.fileCopy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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;

/**
 * 
 * @author L
 * @date 2017年3月14日 下午11:31:33
 *
 */

public class FileCopyDemo {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();

        // copy1("01.04_人机交互.avi", "target_人机交互.avi");
        // copy2("01.04_人机交互.avi", "target_人机交互.avi");
        // copy3("a.txt", "b.txt");
        // copy4("a.txt", "b.txt");
        // copy5("a.txt", "b.txt");
        copy6("a.txt", "b.txt");

        long end = System.currentTimeMillis();
        System.out.println("复制完成,使用: " + (end - start) + " 毫秒");
    }

    /**
     * 字节流方式
     * 
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy1(String path1, String path2) throws IOException {
        // 字节流
        FileInputStream fis = new FileInputStream(path1);
        FileOutputStream fos = new FileOutputStream(path2);

        // 直接数组
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
            fos.flush();
        }

        // 关闭字节流
        fos.close();
        fis.close();
    }

    /**
     * 缓冲区字节流方式
     * 
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy2(String path1, String path2) throws IOException {
        // 缓冲区字节流,速度相对较快
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));

        // 直接数组
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
            bos.flush();
        }

        // 关闭缓冲区字节流
        bos.close();
        bis.close();
    }

    /**
     * 字符流方式
     * 
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy3(String path1, String path2) throws IOException {
        // 字符流读写
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path1));
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path2));

        // 字符数组
        char[] chs = new char[1024];
        int len = 0;
        while ((len = isr.read(chs)) != -1) {
            osw.write(chs, 0, len);
            osw.flush();// 刷新
        }

        // 关闭字符流
        isr.close();
        osw.close();
    }

    /**
     * 文件字符流
     * 
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy4(String path1, String path2) throws IOException {
        // 字符流读写,只需传入String
        FileReader fr = new FileReader(path1);
        FileWriter fw = new FileWriter(path2);

        // 字符数组
        char[] chs = new char[1024];
        int len = 0;
        while ((len = fr.read(chs)) != -1) {
            fw.write(chs, 0, len);
            fw.flush();// 刷新
        }

        // 关闭字符流
        fr.close();
        fw.close();
    }

    /**
     * 缓冲区字符流
     * 
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy5(String path1, String path2) throws IOException {
        // 字符流读写,只需传入String
        BufferedReader br = new BufferedReader(new FileReader(path1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(path2));

        // 字符数组
        char[] chs = new char[1024];
        int len = 0;
        while ((len = br.read(chs)) != -1) {
            bw.write(chs, 0, len);
            bw.flush();// 刷新
        }

        // 关闭字符流
        br.close();
        bw.close();
    }

    /**
     * 使用缓冲区一行行的读文本,非常推荐使用!!!
     * @param path1
     * @param path2
     * @throws IOException
     */
    private static void copy6(String path1, String path2) throws IOException {
        // 字符流读写,只需传入String
        BufferedReader br = new BufferedReader(new FileReader(path1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(path2));

        // 文本一行一行的获取,没有行之后返回null
        String line = br.readLine();// 获取第一行
        while ((line = br.readLine()) != null) {
            bw.write(line);// 写入第一行
            bw.newLine();// 添加换行符
            bw.flush();// 刷新
        }

        // 关闭字符流
        br.close();
        bw.close();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值