IO流(文件复制的方法)

1.使用字符流复制文件的5种方法

package com.dml.io2;
/**
 * 使用字符流的方法复制文件
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Copy {
    public static void main(String[] args) throws Exception{
        method1("Hello.java","1.java");
        method2("Hello.java","2.java");
        method3("Hello.java","3.java");
        method4("Hello.java","4.java");
        method5("Hello.java","5.java");
    }

    /**
     * 使用读单个字符
     * @param read
     * @param writer
     * @throws Exception
     */
    private static void method1(String read, String writer) throws Exception {
        FileReader fr = new FileReader(read);
        FileWriter fw = new FileWriter(writer);

        int ch = 0;
        while((ch = fr.read()) != -1){
            fw.write(ch);
        }
        fw.close();
        fr.close();
    }

    /**
     * 使用读一个数组
     * @param read
     * @param writer
     * @throws Exception
     */
    private static void method2(String read, String writer) throws Exception {
        FileReader fr = new FileReader(read);
        FileWriter fw = new FileWriter(writer);

        char []chs = new char[1024];
        int len = 0;
        while((len = fr.read(chs)) != -1){
            fw.write(chs,0,len);
        }
        fw.close();
        fr.close();
    }

    /**
     * 使用缓冲流读单个字符
     * @param read
     * @param writer
     * @throws Exception
     */
    private static void method3(String read, String writer) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(read));
        BufferedWriter bw = new BufferedWriter(new FileWriter(writer));

        int ch = 0;
        while((ch = br.read()) != -1){
            bw.write(ch);
            bw.flush();
        }
        bw.close();
        br.close();

    }

    /**
     * 使用缓冲流读数组
     * @param read
     * @param writer
     * @throws Exception
     */
    private static void method4(String read, String writer) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(read));
        BufferedWriter bw = new BufferedWriter(new FileWriter(writer));

        char []chs = new char[1024];
        int len = 0;
        while((len = br.read(chs)) != -1){
            bw.write(chs, 0, len);
            bw.flush();
        }
        bw.close();
        br.close();

    }

    /**
     * 使用缓冲流读一行
     * @param read
     * @param writer
     * @throws Exception
     */
    private static void method5(String read, String writer) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(read));
        BufferedWriter bw = new BufferedWriter(new FileWriter(writer));

        String line = null;
        while((line = br.readLine()) != null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        bw.close();
        br.close();

    }
}

2.使用字节流复制文件的4种方法

package com.dml.io2;

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

/**
 * 使用字节流复制文件
 * @author dml
 *
 */
public class Copy_zijie {
    public static void main(String[] args) throws Exception {
        method1("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\1.jpg");
        method2("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\2.jpg");
        method3("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\3.jpg");
        method4("a.jpg","C:\\Users\\dml\\Pictures\\Saved Pictures\\4.jpg");
    }

    private static void method1(String input, String output) throws Exception {
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);

        int by = 0;
        while((by = fis.read()) != -1){
            fos.write(by);
        }
        fos.close();
        fis.close();
    }

    /**
     * 读一个字节数组
     * @param input
     * @param output
     * @throws Exception
     */
    private static void method2(String input, String output) throws Exception {
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);

        byte []bys = new byte[1024];
        int len = 0;
        while((len = fis.read(bys)) != -1){
            fos.write(bys,0,len);
        }
        fos.close();
        fis.close();
    }

    /**
     * 使用缓冲流读一个字节数组
     * @param input
     * @param output
     * @throws Exception
     */
    private static void method3(String input, String output) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));

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

    /**
     * 使用缓冲流读
     * @param input
     * @param output
     * @throws Exception
     */
    private static void method4(String input, String output) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));

        int by = 0;
        while((by = bis.read()) != -1){
            bos.write(by);
        }
        bos.close();
        bis.close();
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值