//CopyFile.java类文件
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.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* @author heyigang
*
*/
public class CopyFile extends Task{
private File src;
private File toDir;
private File filter;
private List srcDirs = new ArrayList(); //存放源目录的路径
private List distDirs = new ArrayList(); //存放目的目录的路径
//默认的文件过滤路径
private String DEFAULT_FILTER_PATH = "filter.ini";
/*
* 构造方法
* */
public CopyFile() {
}
/*
* 加载默认的过滤拷贝的文件路径
* */
public void loadFilter() {
loadFilter(new File(DEFAULT_FILTER_PATH));
}
/*
* 加载配置文件中的过滤拷贝的文件路径
* */
public void loadFilter(File fiter) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filter));
String line = null;
while(null!=(line=reader.readLine())) {
//拼接路径
String srcPath = new StringBuffer(src.getPath())
.append(line.trim()).toString();
String distPath = new StringBuffer(toDir.getPath())
.append(line.trim()).toString();
//判断是否位正确的路径或目录
File tempsrcDir= new File(srcPath);
if(!tempsrcDir.isDirectory() || !tempsrcDir.exists()) {
System.out.println(srcPath + " 不是目录或者目录不存在!");
System.exit(1);
}
//将正确的目录添加到list中
srcDirs.add(srcPath);
distDirs.add(distPath);
}
reader.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}
/*
* 从一个文件目录到另外一个文件目录
* */
public void copyDir(File srcDir,File distDir) {
try {
//创建目标文件目录
if(!distDir.exists()) {
distDir.mkdirs();
}
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++) {
File tempFile = files[i];
if(!tempFile.getName().equals(".svn")) {
if(!tempFile.isDirectory()) {
//是文件开始复制...
copyFile(tempFile,new File(getDistPath(tempFile)));
} else {
//是路径继续调用本身
copyDir(tempFile,new File(getDistPath(tempFile)));
}
}
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
/*
* 根据源文件获得目标文件目录的路径或者全文件名称
* */
public String getDistPath(File srcFile) {
return new StringBuffer(srcFile.getAbsolutePath())
.replace(0, src.getPath().length(), toDir.getPath()).toString();
}
/*
* 从一个文件复制该文件到另外一个目录下面
* */
public void copyFile(File srcFile,File distFile) {
try {
if(!distFile.exists()) {
srcFile.createNewFile();
}
BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(distFile));
byte[] buffer = new byte[1024];
int len = -1;
while(-1!=(len=in.read(buffer))) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}
/*
* Task的可执行方法...
* */
public void execute() throws BuildException {
try {
loadFilter();
System.out.println("开始复制文件,请稍候...");
System.out.println("从目录:" + src.getAbsolutePath());
System.out.println("到目录:" + toDir.getAbsolutePath());
long start = System.currentTimeMillis();
for (int i = 0; i < srcDirs.size(); i++) {
copyDir(new File((String)srcDirs.get(i)), new File((String)distDirs.get(i)));
}
double time = (double)(System.currentTimeMillis() - start) / 1000;
System.out.println("复制文件完成!总共用时" + time + "秒.");
}catch(Exception ex) {
ex.printStackTrace();
}
}
public File getFilter() {
return filter;
}
public void setFilter(File filter) {
this.filter = filter;
}
public File getSrc() {
return src;
}
public void setSrc(File src) {
this.src = src;
}
public File getToDir() {
return toDir;
}
public void setToDir(File toDir) {
this.toDir = toDir;
}
}
//build.xml 配置文件如下
<?xml version="1.0" encoding="GB2312"?>
<project name="CopyFiles" default="copyfiles">
<target name="help">
<echo message="Available targets are:"/>
<echo message="copefiles --> copy files from src to dist"/>
<echo message="-----------------------------------"/>
<echo message="ant.home --> ${ant.home}"/>
<echo message="ant.version --> ${ant.version}"/>
<echo message=""/>
</target>
<taskdef name="copefiles" classname="CopyFile" classpath="."></taskdef>
<target name="copyfiles" depends="help">
<!--
从源目录src复制filter.ini中文件夹下的目录到制定的todir文件目录下
配置文件需要
//-->
<copefiles src="D:/EIP/trunk/Src/Service/erp" todir="D:/Source_V1.2" filter="Source_V1.2.txt"/>
<copefiles src="D:/EIP/trunk/Src/Service/erp" todir="D:/Source_V2.1" filter="Source_V2.1.txt"/>
</target>
</project>
//Source_V1.2.txt 制定需要配置的文件路径
/ot/com/SC_OT_QueryResponsibility
/ot/com/SC_OT_RequestReturnStatusSrv
/ot/com/SC_OT_VerifyAPInvoiceCurrencySvr
/ot/com/SC_OT_VerifyCurrencySvr
/ot/com/SC_OT_COM_VerifyAPInvoiceCurrencySvr
//command.bat 可执行文件
set classpath=%classpath%;%ANT_HOME%\lib\ant.jar;
call javac CopyFile.java
call ant
cd D:\JAVA\apache-ant-1.7.0\hyg
cmd
//所有的配置做好之后,运行command.bat就可以开始进行复制了。
//项目开发过程好过程中,在部署包的时候取包不方便,便有了这个需求,发了10分钟写了CopyFile.java这个核心类。感觉还挺好用。