【java】复制Svn提交列表中所有Class文件至目标文件夹下,并保存原有的路径结构

用于将文件夹下文件全部复制的测试类

优势:

  • 无需导入第三方插件,不受jdk版本限制,
  • 不破坏原有的路径结构,不复制空的文件夹

用法描述:

  1. 找到对应的版本变更文件版本库
  2. 点击比较版本差异
  3. 查看变更文件列表变更的文件列表
  4. 选中“将路径复制到剪切版”保存并新建文件。
  5. 将下图中的readTxtFile 后的路径改为新建文件的路径
  6. 根据代码配置即可

package com.sinsoft.test;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**  
* <p>Title: CopyFile</p>  
* <p>Description: 复制文件</p>  
* @author wangzhen  
* @date 2022年6月27日  
*/
public class CopyFile {
 
    // 允许复制的文件类型
    public static String[] filterFile = { ".jks", ".js", ".jsp", ".class",
            ".java", ".xml", ".properties", ".sql",".jar",".wsdd" };
    private static long total = 0l;
    
    // 复制的源文件文件夹路径
    private final static String orgPathPre = "D:/changan-midAgri/src/main/webapp/";
    
    //目标文件路径
    static String destDirPath="D:/package/daaplatform/";
 
    // private final static String clPth2 = "WebRoot\\WEB-INF\\";
    
    public static void main(String[] args) {
    	  // 读取文本信息
    	try {
    		// 输入变更的文件列表
    		getFile(readTxtFile("D:/changan-midAgri/变更的文件信息-20220112"));
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
        
	}
 
   
 
    /***
    
     * <p>Title: getFile</p>  
     * <p>Description:完成变更文件的复制操作 </p>  
     * @param fileInfoList
     * @throws IOException
     * @throws Exception
     */
    private static void getFile(List<String> fileInfoList) throws IOException, Exception {
        ArrayList<String> tFileCopyFailString = new ArrayList<String>();
        for (String fileInfo : fileInfoList) {
            // System.out.println(string);
            if ("".equals(fileInfo) || fileInfo == null) {
                continue;
            }
            
            if (new File(fileInfo).isDirectory()) {
				continue;
			}
          
            String destFileStr = fileInfo.replace(orgPathPre, "");
            System.out.println("替换后的文件路径"+destFileStr);
            // 指定时间数据
            String df = new SimpleDateFormat("YYYYMMdd").format(new Date());
            File des = null;
            String destPathString=destDirPath+df+File.separator+destFileStr;
            {
            	System.out.println("目的路径"+destPathString);
            	File destFile = new File(destPathString);
            	if (!destFile.exists()) {
            		destFile.getParentFile().mkdirs();
            		destFile.createNewFile();
				}
            	
                des = new File(destPathString);
            }

            System.out.println("源文件路径:"+fileInfo+";生成文件路径:"+destPathString);
        
            try {
            	 copyFile(new File(fileInfo),des);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("复制文件:"+fileInfo+"失败");
				tFileCopyFailString.add(fileInfo);
				continue;
			}
            
          
		}
        System.out.println("下载失败文件个数:"+tFileCopyFailString.size());
        for (String tFailPath : tFileCopyFailString) {
			System.out.println(tFailPath);
          
        }
    }
 


    /***
     * 
    
     * <p>Title: readTxtFile</p>  
     * <p>Description:读取文本信息获取变更的文件信息 </p>  
     * @param filePath
     * @return
     */
    public static List<String> readTxtFile(String filePath) {
        List<String> txtContent = new ArrayList<String>();
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                	String trim = lineTxt.trim();
                	txtContent.add(trim.replace("\\", "\\\\"));

                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return txtContent;
    }
 
 
    

  
 
    /**
     *
     * @param folder
     * @param filterFile
     * @param fileName
     * @throws Exception
     */
    public void copyFolder(File srcFolder, File destFolder,
            String[] filterFile, String fname) throws Exception {
        File[] files = srcFolder.listFiles();
        // System.out.println(destFolder);
        // 先删除旧文件创建目录
        // deleteDir(destFolder);
        destFolder.mkdirs();
 
        for (File file : files) {
            if (file.isFile()) {
                String vl=file.getName().substring(file.getName().lastIndexOf("\\")+1,file.getName().length()).replace(".class", "");
    //          System.out.println("vl:"+vl);
                if (vl.equals(fname.replace(".class", ""))||vl.indexOf(fname.replace(".class", "")+"$")==0) {
                     System.out.println("test:"+file.getName());
                    String pathname = destFolder + File.separator
                            + file.getName();
                    for (String suff : filterFile) {
                        if (pathname.endsWith(suff)) {
                            File dest = new File(pathname);
                            File destPar = dest.getParentFile();
                            destPar.mkdirs();
                            if (!dest.exists()) {
                                dest.createNewFile();
                            }
                            // D:\springmvc\WebRoot\WEB-INF\index.jsp
                            // D:\java\20160603\WebRoot\index.jsp
                            // 为了防止重命名并且不在同一个路径下COPY
                            if ((file.getParent()
                                    .substring(file.getParent().length() - 4,
                                            file.getParent().length() - 1)
                                    .equals(dest.getParent().substring(
                                            dest.getParent().length() - 4,
                                            dest.getParent().length() - 1)))||file.getParent().contains("conf")) {
                                if (file.length() == 0) {
                                    throw new IOException("文件不允许为空"
                                            + ",需要处理的文件为:" + file.getParent()
                                            + "\\" + file.getName());
                                }
                                copyFile(file, dest);
                            }
 
                        }
                    }
                }
            } else {
                copyFolder(file, destFolder, filterFile, fname);
            }
        }
    }
 
    /***
     * copy file
     *
     * @param src
     * @param dest
     * @throws IOException
     */
    private static void copyFile(File src, File dest) throws Exception {
        BufferedInputStream reader = null;
        BufferedOutputStream writer = null;
 
        try {
            reader = new BufferedInputStream(new FileInputStream(src));
            writer = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] buff = new byte[reader.available()];
            while ((reader.read(buff)) != -1) {
                writer.write(buff);
            }
            total += 1;
        } catch (Exception e) {
            throw e;
        } finally {
            writer.flush();
            writer.close();
            reader.close();
            // 记录
            String temp = "\ncopy:\n" + src + "\tsize:" + src.length()
                    + "\nto:\n" + dest + "\tsize:" + dest.length()
                    + "\n complate\n totoal:" + total;
            System.out.println(temp);
        }
    }
 
    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     *
     * @param dir
     *            将要删除的文件目录 暂时还没用
     */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
 
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空,可以删除
        return dir.delete();
    }
    
    
    public static void copyFile(String oldPath, String newPath) 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];
        int readByte = 0;
        while((readByte = in.read(buffer)) != -1){
            out.write(buffer, 0, readByte);
        }
    
        in.close();
        out.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值