读写文件夹以及文件并复制到指定目录中

package chaincode;
import java.io.*;
import java.util.Scanner;


public class read_write {

/**
* 复制文件夹
* @param oldPath 老文件路径
* @param newPath 新文件路径
* @param number 编号
* @throws IOException
*/
public static void copyDir(String oldPath, String newPath, int number) throws IOException {
        File file = new File(oldPath);
        String[] filePath = file.list();
        
        if (!(new File(newPath)).exists()) {
            new File(newPath).mkdir();
        }
        
        for (int i = 0; i < filePath.length; i++) {
            if ((new File(oldPath + file.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath  + file.separator  + filePath[i], newPath  + file.separator + filePath[i]+number,number);
            }
            
            if (new File(oldPath  + file.separator + filePath[i]).isFile()) {
                copyFile(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i].replaceAll("1.go", "")+number+".go",number);//修改复制的文件名
            }
            
        }
    }

/**
* 复制文件
* @param oldPath 老文件路径
* @param newPath 新文件路径
* @param number 编号
* @throws IOException
*/
public static void copyFile(String oldPath, String newPath,int number) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);;


        byte[] buffer=new byte[2097152];
        
        while((in.read(buffer)) != -1){
            out.write(buffer);
        }
    
        
    }

public static void main(String[] args) throws IOException {
        
        String oldPath = "E:\\test1";
        for(int i=1; i <= 1; i++) {
        String newPath= "E:\\test2";
        copyDir(oldPath, newPath+i, i);
        }


        
      
       
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值