复制改进

来源:互联网 发布:算法设计与分析考试题 编辑:程序博客网 时间:2024/05/21 13:59
public void copy(File f1,File f2) throws Exception
{
if(f1.isDirectory())
{
File[] list = f1.listFiles();
String path = f2.getPath()+File.separator;
for(int i=0;i<list.length;i++)
{
if(list[i].isDirectory())
{
new File(path+list[i].getName()).mkdir();
}
copy(list[i],new File(path+list[i].getName()));
}
}
else
{
InputStream input=new FileInputStream(f1);  
       OutputStream output=new FileOutputStream(f2);  
       if((input!=null)&&(output!=null)){  
           int temp=0;  
           while((temp=input.read())!=(-1)){  
               output.write(temp);  
           }  
       }  
       input.close();  
       output.close();
}
}