public class doFile {
private void copyfile(String str,String str1) throws FileNotFoundException {
try {
File file=new File(str);
InputStream is=null;
OutputStream os=null;
byte[] b;
File f=new File(str1);
if(!file.isDirectory()) {
is=new FileInputStream(str);
b=new byte[is.available()];
is.read(b);
os=new FileOutputStream(str1);
os.write(b);
is.close();
os.close();
return;
} else if(!f.exists())
f.mkdirs();
File[] filename=file.listFiles();
for(int i=0;i<filename.length;i++) {
copyfile(filename[i].getAbsolutePath(),str1+"/"+filename[i].getName());
}
} catch(IOException ex) {
filecopycount++;
System.out.println("filecopycount:"+String.valueOf(filecopycount));
if(filecopycount<=5)
copyfile(str,str1);
System.err.println("err:"+ex.toString());
}
}
}
调用:
new doFile().copyfile("e:\\fun\\help.swf","c:\\like\\kill.swf");
当然这段代码还可以copy一个目录
private void copyfile(String str,String str1) throws FileNotFoundException {
try {
File file=new File(str);
InputStream is=null;
OutputStream os=null;
byte[] b;
File f=new File(str1);
if(!file.isDirectory()) {
is=new FileInputStream(str);
b=new byte[is.available()];
is.read(b);
os=new FileOutputStream(str1);
os.write(b);
is.close();
os.close();
return;
} else if(!f.exists())
f.mkdirs();
File[] filename=file.listFiles();
for(int i=0;i<filename.length;i++) {
copyfile(filename[i].getAbsolutePath(),str1+"/"+filename[i].getName());
}
} catch(IOException ex) {
filecopycount++;
System.out.println("filecopycount:"+String.valueOf(filecopycount));
if(filecopycount<=5)
copyfile(str,str1);
System.err.println("err:"+ex.toString());
}
}
}
调用:
new doFile().copyfile("e:\\fun\\help.swf","c:\\like\\kill.swf");
当然这段代码还可以copy一个目录
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货