`
superxielei
  • 浏览: 262287 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

快速复制java对应的class文件,方便制作svn的patch。

    博客分类:
  • java
阅读更多

最近手里好几个项目都在频繁的更新,我一直使用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();
	}
}

分享到:
评论
4 楼 qq705570553 2017-01-18  
嗯,是可以用,注意要保持两个文件夹的一致性
3 楼 qq705570553 2016-09-13  
path1里面的java文件根本就没替换成class
2 楼 qq705570553 2016-09-13  
用不了,是不是你写的有问题?
1 楼 yutou0921 2013-06-17  
你好,最近也遇到一样的问题,也是做了这么一个拷贝的小程序。
但是不知道您有没发现,拷贝出来的class文件是错误的,用反编译软件打开报错,跑工程的时候也会报错。
不知道您是怎么解决的?

相关推荐

Global site tag (gtag.js) - Google Analytics