手动输入路径进行复制并修改文件后缀,灵活好用

手动输入路径进行复制并修改文件后缀,灵活好用

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class copyUpdateFile {
    static String  start=null;
     static  String end=null;
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入源路径");
        String  oldP= scanner.nextLine();
        System.out.println("请输入目标路径");
        String newP = scanner.nextLine();
        System.out.println("请输入你要修改的后缀,例:"+"\t"+".txt");
         start=scanner.nextLine();
        System.out.println("请输入目标后缀");
        end=scanner.nextLine();
        String regex="[a-zA-Z]:(((\\\\(?! )[^/:*?<>\\\"\"|\\\\]+)+\\\\?)|(\\\\)?)\\s*";
        if (oldP.matches(regex)&&newP.matches(regex)){
            File oldPath=new File(oldP);
            File newPath=new File(newP);
            if (!newPath.exists()){
                newPath.mkdirs();
            }
            copyFile(oldPath,newPath);
            System.out.println("复制成功");
        }else {
            System.out.println("文件路径错误");
        }

    }

    private static void copyFile(File oldPath, File newPath) throws IOException {
           if (oldPath.exists()){
               File[] files = oldPath.listFiles();
               for (File data:
                    files) {
                   if(data.isFile()){
                       File file=new File(newPath,data.getName());
                       copyNewFile(data,file);
                       if(file.getName().endsWith(start)){
                           String absolutePath = file.getAbsolutePath();
                                           absolutePath.substring(0,absolutePath.lastIndexOf("."));
                           File file1=new File(absolutePath+end);
                           file.renameTo(file1);
                       }
                   }else {
                       File file=new File(newPath,data.getName());
                       file.mkdirs();
                       copyFile(data,file);
                   }
               }
           }
    }

    private static void copyNewFile(File data, File fis) {
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream=new FileInputStream(data);
            File file=new File(fis+"");
            fileOutputStream=new FileOutputStream(file);
            int len=0;
            byte[] bytes=new byte[1024*8];
            while ((len=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
                fileOutputStream.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}



请输入源路径
C:\Users\Administrator\Desktop\a
请输入目标路径
D:\t
请输入你要修改的后缀,:+	.txt
.txt
请输入目标后缀
.rar
复制成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值