copy文件到指定目录下面工具类

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* copy 文件到指定目录下面,并创建文件目录
* @author wxh64788665@yahoo.cn 20120818:begin
*
*/
public class GenerateDirecoryUtil {

/**
* web 项目的class路径
*/
private final static File fileClass=new File("D:/eclipseForWeb/code/WebApp/WebContent/WEB-INF/classes");
/**
* web 项目的类路径
*/
private final static File fileCommon=new File("D:/eclipseForWeb/code/WebApp/WebContent");
/**
* copy的文件存在于这个目录下面,不必创造这个目录
*/
private final static File fileLocation=new File("D:/generate");
/**
* class文件路径
*/
private final static String STRINGDIRECORYClass[] = { "com.bq.action.ArticleActionTest.class","com.bq.action.JxlTestAction.class"};
/**
* 其它资源文件路径
*/
private final static String STRINGDIRECORYCommon[] = { "pages.vba.Combine_vba.jsp"};


public static void main(String[] args) throws Exception {
GenerateToDirecory();
}

public static void GenerateToDirecory() throws Exception
{

/**
* class文件路径中的.替换成/
*/
for (int i = 0; i < STRINGDIRECORYClass.length; i++)
STRINGDIRECORYClass[i] = STRINGDIRECORYClass[i].replace(".", "/");
/**
* 其它资源文件路径中的.替换成/
*/
for (int j = 0; j < STRINGDIRECORYCommon.length; j++)
STRINGDIRECORYCommon[j] = STRINGDIRECORYCommon[j].replace(".", "/");


/**
* class文件路径中的最后的/替换成.因为最后是class文件,不必生成目录
*/
for(int k=0;k<STRINGDIRECORYClass.length; k++)
{
int m=STRINGDIRECORYClass[k].lastIndexOf("/");
String oldStr=STRINGDIRECORYClass[k].substring(m, STRINGDIRECORYClass[k].length());
String newStr=STRINGDIRECORYClass[k].substring(m+1);
STRINGDIRECORYClass[k]=STRINGDIRECORYClass[k].replaceAll(oldStr, "."+newStr);
}
/**
* 其它资源文件路径中的路径中的最后的/替换成.因为最后是class文件,不必生成目录
*/
for(int n=0;n<STRINGDIRECORYCommon.length; n++)
{
int j=STRINGDIRECORYCommon[n].lastIndexOf("/");
String oldStr=STRINGDIRECORYCommon[n].substring(j, STRINGDIRECORYCommon[n].length());
String newStr=STRINGDIRECORYCommon[n].substring(j+1);
STRINGDIRECORYCommon[n]=STRINGDIRECORYCommon[n].replaceAll(oldStr, "."+newStr);
}


/**
* 将日期转换成指定格式
*/
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd");
String sdf=simpleDateFormat.format(new Date());

/**
* 循环将指定文件copy到相应的目录下面
*/
for (int l = 0; l < STRINGDIRECORYClass.length; l++) {
File f = new File(new File(fileLocation,sdf),STRINGDIRECORYClass[l]);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
copyFile(new File(fileClass,STRINGDIRECORYClass[l]),f);
}

/**
* 循环将资源文件copy到相应的目录下面
*/
for (int k = 0; k < STRINGDIRECORYCommon.length; k++) {
File f = new File(new File(fileLocation,sdf),STRINGDIRECORYCommon[k]);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
copyFile(new File(fileCommon,STRINGDIRECORYCommon[k]),f);
}
}

/**
*
* @param fileIn 要被copy的文件
* @param fileOutPut 将文件copy到那个目录下面
* @throws Exception
*/
private static void copyFile(File fileIn,File fileOutPut) throws Exception
{
FileInputStream fileInputStream=new FileInputStream(fileIn);
FileOutputStream fileOutputStream=new FileOutputStream(fileOutPut);
byte[] by=new byte[1024];
int len;
while((len=fileInputStream.read(by))!=-1)
{
fileOutputStream.write(by, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值