java复制文件,将一些文件复制到新的工作空间

来源:互联网 发布:算法工程师考试题目 编辑:程序博客网 时间:2024/04/29 07:13

java复制文件,

使用环境:

有时候svn分支变换很快,这时原来workspace中的一些配置信息测试类需要手工复制到新的工作空间中,而有了这个类,只需要把修改过的文件移到目录下,运行程序就行了,不用在手工拷贝代码。目录位置根据需要修改,项目名称根据需要修改


import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;




/**
 * @date 2016年10月31日 上午9:26:19
 */
public class MvToNewWorkspace {
   
    public final String CORE = "core\\";

    final String targetWorkPath = "workspace65test";
    String targetPath = "D:\\tools\\" + targetWorkPath + "\\";
    final String srcPath = "C:\\Users\\Administrator\\Desktop\\工作\\新workspace调整\\";
    final boolean OverLay = true;
    final String javaFolder = "src\\";
    final String resFolder = "resources\\";
    final String webxmlFolder = "WebContent\\WEB-INF\\";
    final String resFolderCore="src\\main\\resources\\";


    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("start");
        new MvToNewWorkspace().start();
        System.out.println("end");
    }


    public void start() {
  
        mv(srcPath , CORE);
 
    }


    public void mv(String waitedMvFolder,String subFolder) {
        File srcDir = new File(waitedMvFolder+subFolder);
        File[] files = srcDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File file : files) {
            if (file.getName().endsWith(".java")) {
                copy(file.getAbsolutePath(), targetPath + subFolder+javaFolder + getPackage(file)+"\\"+file.getName(), OverLay);
            } else if (file.getName().endsWith(".properties")) {
                if (subFolder.equals(CORE)) {//src\main\resources
                    copy(file.getAbsolutePath(), targetPath +subFolder+ resFolderCore + file.getName(), OverLay);
                }else {
                    copy(file.getAbsolutePath(), targetPath +subFolder+ resFolder + file.getName(), OverLay);
                }
                
            } else if (file.getName().equals("web.xml")) {
                copy(file.getAbsolutePath(), targetPath + subFolder+webxmlFolder +file.getName(), OverLay);
            }
        }
    }


    public String getPackage(File file) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // System.out.println("line " + line + ": " + tempString);
                if (tempString.contains("package")) {
                    tempString = tempString.replaceAll("[\\s;]|package", "");
                    tempString = tempString.replace(".", "\\");
                    return tempString;
                }
                line++;
                if (line > 20) {
                    return "";
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return "";
    }


    public static boolean copy(String srcFileName, String destFileName, boolean overlay) {
        File srcFile = new File(srcFileName);
        String message= "";
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            message= "源文件:" + srcFileName + "不存在!";
            System.out.println(message);
            return false;
        } else if (!srcFile.isFile()) {
            message= "复制文件失败,源文件:" + srcFileName + "不是一个文件!";
            System.out.println(message);
            return false;
        }


        // 判断目标文件是否存在
        File destFile = new File(destFileName);
        if (destFile.exists()) {
            // 如果目标文件存在并允许覆盖
            if (overlay) {
                // 删除已经存在的目标文件,无论目标文件是目录还是单个文件
                new File(destFileName).delete();
            }
        } else {
            // 如果目标文件所在目录不存在,则创建目录
            if (!destFile.getParentFile().exists()) {
                // 目标文件所在目录不存在
                if (!destFile.getParentFile().mkdirs()) {
                    // 复制文件失败:创建目标文件所在目录失败
                    return false;
                }
            }
        }
        copy(srcFile, destFile);
        return true;
    }


    public static boolean copy(File srcFile, File destFile) {
        FileChannel fileChannelIn = null;
        FileChannel fileChannelOut = null;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);
            fileChannelIn = fileInputStream.getChannel();
            fileChannelOut = fileOutputStream.getChannel();
            fileChannelIn.transferTo(0, fileChannelIn.size(), fileChannelOut);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fileChannelOut.close();
                fileChannelIn.close();
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
            }
        }
        return true;
    }


}
0 0
原创粉丝点击