static void cpyFolder(String source,String dest) {
//实现复制整个文件夹
File fs=null;
File fd=null;
//InputStream is=null;
//OutputStream os=null;
try {
if(source==null || dest ==null) {
throw new Exception("param :source Or dest incorrect!");
}
fs=new File(source);
fd=new File(dest);
if(!fs.exists()) {
throw new Exception("source file not exist!");
}
if(!fs.isDirectory()) {
throw new Exception("source file is not folder!");
}
if(fd.exists()) {
throw new Exception("dest file already exist!");
}
if(!fd.mkdirs()) {
throw new Exception("creat dest folder failed!");
}
cpyFold(fs,fd);
}
catch(Exception e) {
e.printStackTrace();
}
}
static void cpyFold(File s,File d) {
//复制的具体复制实现
if(s.isDirectory()) {
if(!d.exists()) {
if(!d.mkdirs()) {
System.out.println("MakeDir Failed!");
}
}
File[] tmp=s.listFiles();
for(int i=0;i
String
name=d.getAbsolutePath()+File.separator+tmp[i].getName();
System.out.println(name);
cpyFold(tmp[i],new File(name));
}
}
else {
if(!d.exists()) {
cpyFile(s,d);
}
}
}
static void cpyFile(File fs,File fd) { //复制单个文件
InputStream is=null;
OutputStream os=null;
try {
is=new FileInputStream(fs);
os=new FileOutputStream(fd);
byte[] buf=new byte[1024];
int n=0;
while(true) {
n=is.read(buf, 0, 1024);
if(-1==n)
break;
os.write(buf,0,n);
}
//System.out.println("COPY "+fs.getAbsolutePath()+" TO
"+fd.getAbsolutePath()+" OK!");
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(null!=is ) {
is.close();
is=null;
}
if(null!=os) {
os.close();
os=null;
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}