快速复制java对应的class文件,方便制作svn的patch。
最近手里好几个项目都在频繁的更新,我一直使用svn的patch制作补丁,但是因为class是不受svn管理的,每次更新的类都要手动的一个一个复制出来,少的时候没感觉怎么样,最近太多了,弄的我焦头烂额的,所以写了个小东西,自动根据已有的java文件,找对应的class复制过来。这样果然方便多了。
?
?
package com.radium.createpatch;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;/** * 创建补丁 * * @author XieLei * @update 2011-11-9 下午2:15:45 */public class CreatePatch {static String path1 = "C:\\Users\\Radium-Work\\Desktop\\2011-11-9 84-140\\WebRoot\\WEB-INF\\classes";static String path2 = "D:\\WorkSpace\\neuedu-metallurgy\\trunk\\neuedu-metallurgy\\WebRoot\\WEB-INF\\classes";public static void main(String[] args) {File file1 = new File(path1);File file2 = new File(path2);if (file1.exists() && file2.exists()) {createPath(file1);}System.out.println("共复制:"+count);}static File tempFile1;static File tempFile2;static String tempStr;static int count = 0;public static void createPath(File file) {if (file.exists()) {File[] files = file.listFiles();for (File f : files) {if (f.isDirectory()) {createPath(f);} else {if (f.getName().endsWith(".java")) {count++;tempStr = path2 + f.getPath().substring(path1.length(), f.getPath().length());tempStr = tempStr .substring(0, tempStr.lastIndexOf(".")) + ".class";tempFile1 = new File(tempStr);tempFile2 = new File(f.getPath().substring(0, f.getPath().lastIndexOf("\")+1)+f.getName().subSequence(0, f.getName().lastIndexOf(".")) +".class");if(tempFile1.exists()){try {copyFile(tempFile1,tempFile2);f.delete();} catch (Exception e) {e.printStackTrace();}}System.out.println(f.getName());}}}}}/** * 将F1复制到F2 * @param f1 * @param f2 * @return * @throws Exception * @author XieLei * @update 2011-11-9 下午2:42:09 */public static void copyFile(File f1, File f2) throws Exception {int length = 2097152;FileInputStream in = new FileInputStream(f1);FileOutputStream out = new FileOutputStream(f2);byte[] buffer = new byte[length];int ins = 0;while (true && ins != -1) {ins = in.read(buffer);if(ins != -1)out.write(buffer, 0, ins);}in.close();out.flush();out.close();}}