六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 16|回复: 0

拷贝一个目录(文件)到指定路径

[复制链接]

升级  60%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
30
 楼主| 发表于 2013-1-28 19:31:51 | 显示全部楼层 |阅读模式
/**     *拷贝一个目录或者文件到指定路径下     *@paramsource     *@paramtarget     */     public void copy(File source,File target) {       File tarpath = new File(target,source.getName());       if(source.isDirectory())  {           tarpath.mkdir();           File[] dir = source.listFiles();           for (int i = 0; i < dir.length; i++) {               copy(dir[i],tarpath);           }       }else  {           try {               InputStream is = new FileInputStream(source);               OutputStream os = new FileOutputStream(tarpath);               byte[] buf = newbyte[1024];               int len = 0;               while((len = is.read(buf))!=-1) {                   os.write(buf,0,len);               }               is.close();               os.close();           } catch (FileNotFoundException e) {               // TODO Auto-generated catch block               e.printStackTrace();           } catch (IOException e) {               // TODO Auto-generated catch block               e.printStackTrace();           }       }     } }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表