public class Xcopy
{
private File dir;
private Logger log = Logger.getLogger(Xcopy.class);
protected ArrayList<File> fileList = new ArrayList<File>();
/**
*
* @param dir 需要找的文件所在目录
*/
public Xcopy(File dir)
{
setDir(dir);
}
public Xcopy()
{
}
public File getDir()
{
return dir;
}
/**
*
* @param dir 需要找的文件所在目录
*/
public void setDir(File dir)
{
if(dir == null || !dir.exists() || dir.isFile())
{
log.info("请输入目录名称!");
System.exit(0);
}
this.dir = dir;
}
/**
*
* @param desDir 需要把文件拷入的文件夹,可以不存在,自动创建
*/
public void xcopy(String desDir)
{
Runtime runtime = Runtime.getRuntime();
if(!desDir.endsWith("\\"))
{
desDir = desDir.trim()+"\\";
}
for(File file : fileList)
{
try
{
runtime.exec("Xcopy /D /S /Y "+file.getAbsolutePath()+" "+desDir).waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
*
* @param suffix 需要的文件后缀
* @return 如果找到,返回true,否则false
*/
public boolean find(String suffix)
{
for(File file : dir.listFiles())
{
if(file.isFile())
{
if(file.getName().endsWith(suffix))
{
fileList.add(file);
}
}
else
{
dir = file;
find(suffix);
}
}
if(fileList.size() != 0)
{
return true;
}
return false;
}
public static void main(String[] args)
{
Xcopy xcopy = new Xcopy(new File("d:\\new\\src"));
if(xcopy.find("java"))
{
xcopy.xcopy("d:\\new\\jxl");
}
}
}