免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1494 | 回复: 0
打印 上一主题 下一主题

java复制文件,文件夹,以及嵌套文件夹 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-09 13:03 |只看该作者 |倒序浏览
文件复制
  1. package file;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;

  11. /**
  12. * 提供java   IO  操作
  13. * @author ronaldoGT
  14. *
  15. */
  16. public class IOfile {

  17.     /**
  18.      * @param args
  19.      */
  20.     private static IOfile ioFile = new IOfile();
  21.     private static String oldUrl = "E://FunshionMedia/黑客帝国/老梁观世界-20130311.mp4";
  22.     private static String newUrl = "E://老梁观世界-20130311.mp4";
  23.      
  24.     public static void main(String[] args) {   
  25.         ioFile.copyFile(new File(oldUrl),new File(newUrl));
  26.         ioFile.copyDirectory(new File("D:/html5"),"E:/");
  27.         System.out.println("ok!");
  28.     }
  29.      
  30.     /**
  31.      * 复制文件夹,嵌套文件夹,以及包含的文件
  32.      * @param oldDirectory
  33.      * @param newUrl
  34.      */
  35.     public void copyDirectory(File oldDirectory,String newUrl){
  36.         File[] f=oldDirectory.listFiles();
  37.         File file = new File(newUrl);
  38.         String url;
  39.         url = oldDirectory.getName();
  40.         newUrl += "/"+url;
  41.         file = new File(newUrl);
  42.         file.mkdir();
  43.         for(File ff:f){
  44.             if(ff.isDirectory()){
  45.                 copyDirectory(ff,newUrl);
  46.             }
  47.             if(ff.isFile()){
  48.                 copyFile(ff,new File(newUrl+"/"+ff.getName()));
  49.             }   
  50.         }
  51.      
  52.     }
  53.      
  54.      
  55.     /**
  56.      * 复制文件
  57.      * @param oldFile
  58.      * @param newFile
  59.      */
  60.     public void copyFile(File oldFile,File newFile){
  61.         InputStream fis = null;
  62.         OutputStream fos = null;
  63.         try {
  64.             fis = new BufferedInputStream(new FileInputStream(oldFile));
  65.             fos = new BufferedOutputStream(new FileOutputStream(newFile));
  66.             byte[] buf = new byte[1024*2];
  67.             int len ;
  68.             while((len = fis.read(buf)) != -1){
  69.                 fos.write(buf, 0, len);
  70.             }
  71.             fos.flush();
  72.         } catch (Exception e) {
  73.             // TODO Auto-generated catch block
  74.             e.printStackTrace();
  75.         }finally{
  76.             try {
  77.                 fis.close();
  78.                 fos.close();
  79.             } catch (IOException e) {
  80.                 // TODO Auto-generated catch block
  81.                 e.printStackTrace();
  82.             }
  83.         }
  84.     }

  85. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP