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();
}
}
copy文件到指定目录下面工具类
最新推荐文章于 2023-09-10 21:09:39 发布