手動輸入路徑進行復制並修改檔案後綴,靈活好用

2020-08-14 01:04:38

手動輸入路徑進行復制並修改檔案後綴,靈活好用

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
複製成功