通过File类和I/O流实现一个文件复制功能

描述
  • 将路径E:\pjava下的文件全部复制到F:\下
思路
  • 通过I/O流和File类进行实现
  1. 根据指定路径生成两个file对象
File start = new File("E:\\pjava");
  File end = new File("F:\\");
  1. 实现删除方法 void move(File start, File end),传入要操作的两个文件对象,获取源文件的文件对象列表,和排序文件列表为空的情况
File[] sFile = start.listFiles();
 if(sFile == null) return;
  1. 遍历文件对象列表,如果是文件的话,就接着使用I/O流,将相关文件复制到目的路径下
if(file.isFile()){
                //在end路径下生成一个相同的文件
                File file1 = new File(end, file.getName());
                //创建i/o流
                FileInputStream rStream = null;
                FileOutputStream eStream = null;

                //获得文件名
                try {
                    rStream = new FileInputStream(file.getPath());
                    eStream = new FileOutputStream(file1.getPath());

                    //写入数据
                    int len;
                    //每次读1024kb的十倍的长度
                    byte[] bys = new byte[1024*10];
                    while((len = rStream.read(bys)) != -1 ){
                        //往目标文件写入数据
                        eStream.write(bys);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
}
  1. 是文件夹,就在目的路径下生成对应文件夹
/是文件夹就继续搬动,并创建文件夹
                String name = file.getName();
                String ePath = end.getPath() + "/" + name;
                File f = new File(ePath);
                //创建文件夹
                f.mkdir();
  1. 递归该文件夹下的文件,复制到目的路径下对应的文件夹
move(file,end);
  • 总代码:
import java.io.*;

public class file_test2_copy {
    public static void main(String[] args) throws IOException {
        File start = new File("E:\\pjava");
        File end = new File("F:\\");
        move(start,end);

    }

    private static void move(File start, File end) {
        File[] sFile = start.listFiles();
        if(sFile == null) return;
        //创建原文件夹

        for (File file : sFile) {
            //该文件对象是文件
            //往文件输入内容
            if(file.isFile()){
                //在end路径下生成一个相同的文件
                File file1 = new File(end, file.getName());
                //创建i/o流
                FileInputStream rStream = null;
                FileOutputStream eStream = null;

                //获得文件名
                try {
                    rStream = new FileInputStream(file.getPath());
                    eStream = new FileOutputStream(file1.getPath());

                    //写入数据
                    int len;
                    byte[] bys = new byte[1024*10];
                    while((len = rStream.read(bys)) != -1 ){
                        //往目标文件写入数据
                        eStream.write(bys);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(rStream != null){
                        try {
                            rStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(eStream != null){
                        try {
                            eStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                //是文件夹就继续搬动,并创建文件夹
                String name = file.getName();
                String ePath = end.getPath() + "/" + name;
                File f = new File(ePath);
                //创建文件夹
                f.mkdir();
                //递归循环
                move(file,end);
            }
        }
    }
}

  • 结果
  • 运行前
    在这里插入图片描述
  • 运行后
    在这里插入图片描述
注意
  • 输出输入流,要看谁是主体
  • 这里我们写的test源文件是主体,从外面读文件进test源文件是输入流
    • 输入流的是read()方法,获得的是byte字节数据,是一串数字
  • 从test源文件写数据到外面文件是输出流
    • 输出流的是write()方法,是传入字节数据,将byte字节数据写入到相关文件中去,在写入时,会转换为它本来代表的含义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值