IO流文件的拷贝

<pre name="code" class="java">package com.zhu.io;
import java.io.*;

/**
 * Created by idea on 2016/6/6.
 * @author 我就是你们的小星星
 *
 * 使用字节流拷贝文件(只能作用于纯文本)
 */
@SuppressWarnings("all")
public class CopyFile2 {
    public static void main(String[] args) {
        String srcPath = "d:/testio/a.txt";
        String descPath = "d:/testio/c.txt";
        copyFile(srcPath , descPath);
    }
    public static void copyFile(String srcPath , String descPath){
        Reader reader = null;
        Writer writer = null;
        try {
            File src = new File(srcPath);
            File desc = new File(descPath);
            reader = new BufferedReader(new FileReader(src));
            writer = new BufferedWriter(new FileWriter(desc));
            char [] data = new char [1024];
            int len = 0 ;
            while (-1 != (len = reader.read(data))) {
                writer.write(data , 0 , len);
                writer.flush();
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                    writer = null;
                }
                if (reader != null ) {
                    reader.close();
                    reader = null;
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

package com.zhu.io;import java.io.*;/** * Created by idea on 2016/6/6. * @author 我就是你们的小星星 */public class CopyFile { public static void main(String[] args) { String srcPath = "d:/testio/a.txt"; String descPath = "d:/testio/b.txt"; copyFile(srcPath , descPath); } public static void copyFile(String srcPath , String descPath){ InputStream is = null; OutputStream os = null; try { File src = new File(srcPath); File desc = new File(descPath); is = new BufferedInputStream(new FileInputStream(src)); os = new BufferedOutputStream(new FileOutputStream(desc)); byte [] data = new byte[1024]; int len = 0; while (-1 != (len = is.read(data))){ os.write(data , 0 , len); os.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (os != null) { os.close(); os = null; } if (is != null) { is.close(); is = null; } } catch (IOException e) { e.printStackTrace(); } } }}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值