A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mk7 中级黑马   /  2013-6-12 17:18  /  2192 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为了将文件夹从d盘根目录复制到e盘根目录,写了以下代码。
  1. public static void main(String[] args) throws IOException
  2. {
  3. File file = new File("d:\\java");
  4. copy(file);
  5. }

  6. public static void copy(File file) throws IOException
  7. {
  8. //获取文件夹内的文件列表
  9. File[] files = file.listFiles();

  10. //遍历文件夹内容
  11. for (File file2 : files)
  12. {
  13. //判断是否是子文件夹
  14. if(file2.isDirectory())
  15. {
  16. copy(file2);
  17. }

  18. //定义文件复制过程中的字节流
  19. FileInputStream fis = new FileInputStream(file2);
  20. FileOutputStream fos = new FileOutputStream("此处应该怎样指定目录??");

  21. //复制文件
  22. byte[] buf = new byte[1024];
  23. int len = 0;
  24. while((len=fis.read(buf))!=-1)
  25. {
  26. fos.write(buf, 0, len);
  27. }
  28. }
  29. }
复制代码
将文件夹从d盘根目录复制到e盘根目录的过程中,为了保持复制后的层级目录关系,复制时写入流应该指定好目的目录,
其子文件夹和文件的目录应该怎样处理?

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

8 个回复

倒序浏览
本帖最后由 许聪聪 于 2013-6-12 17:46 编辑

我自己写的,希望对楼主有用

  1. import java.io.BufferedInputStream;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. /**
  7. * 编写程序拷贝一个文件, 尽量使用效率高的方式.
  8. */
  9. public class Test6{  
  10.   
  11.     public static void main(String[] args) throws IOException {  
  12.         String src_file = "E:/java/test.doc";  
  13.         String des_file = "E:/java/test_copy.doc";  
  14.          
  15.         copyFile(src_file, des_file);  
  16.          
  17.         System.out.println("OK!");  
  18.     }  
  19.   
  20.     public static void copyFile(String src, String des) throws IOException {  
  21.         BufferedInputStream inBuff = null;  
  22.         BufferedOutputStream outBuff = null;  
  23.         try {  
  24.             // 新建文件输入流并对它进行缓冲  
  25.             inBuff = new BufferedInputStream(new FileInputStream(src));  
  26.             // 新建文件输出流并对它进行缓冲  
  27.             outBuff = new BufferedOutputStream(new FileOutputStream(des));  
  28.             // 缓冲数组  
  29.             byte[] b = new byte[600 * 5];  
  30.             int len;  
  31.             while ((len = inBuff.read(b)) != -1) {  
  32.                 outBuff.write(b, 0, len);  
  33.             }  
  34.             // 刷新此缓冲的输出流  
  35.             outBuff.flush();  
  36.         } finally {  
  37.             // 将流关闭
  38.             if (inBuff != null)  
  39.                 inBuff.close();  
  40.             if (outBuff != null)  
  41.                 outBuff.close();  
  42.         }
  43.     }  
  44. }  
复制代码

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

回复 使用道具 举报
尹桥印 发表于 2013-6-12 17:26
getAbsolutePath() 返回此抽象路径名的绝对路径名字符串,将字符串里面的指定内容用replace()方法替换之后 ...

还有别的方法吗
回复 使用道具 举报
过来围观,不会1
回复 使用道具 举报
本帖最后由 Heart 于 2013-6-13 13:44 编辑
  1.                 InputStreamReader isr = null;
  2.                 OutputStreamWriter osw = null;
  3.                
  4.                 BufferedReader br = null;
  5.                 BufferedWriter bw = null;
  6.                 try {   
  7.                         isr = new InputStreamReader(new FileInputStream("D:\\java1.txt"), "UTF-8") ;
  8.                         osw = new OutputStreamWriter(new FileOutputStream("D:\\java2.txt"), "UTF-8") ;
  9.                         
  10.                         //加入了缓冲技术,对字节输入流对象再进行包装
  11.                         br = new BufferedReader(isr);
  12.                         //加入了缓冲技术,对字节输出流对象再进行包装
  13.                         bw = new BufferedWriter(osw);
  14.                         
  15.                         String line = null;
  16.                         while((line=br.readLine())!=null)
  17.                         {
  18.                                 bw.write(line);
  19.                                 bw.newLine();
  20.                                 bw.flush();
  21.                         }
  22.                 } catch (FileNotFoundException e) {
  23.                         System.out.println("文件未找到!");
  24.                 } catch (IOException e) {
  25.                         System.out.println(e.toString());
  26.                 }
  27.                
  28.                 finally {
  29.                         try {
  30.                                 if(bw!=null)
  31.                                         bw.close();        //关闭流
  32.                                 if(br!=null)
  33.                                         br.close();
  34.                         } catch (IOException e) {
  35.                                 System.out.println(e.toString());
  36.                         }
  37.                 }
复制代码
回复 使用道具 举报

我想复制一个文件夹 而不是单纯的一个文本文件  你这个好像实现不了啊  
回复 使用道具 举报
许聪聪 发表于 2013-6-12 17:30
我自己写的,希望对楼主有用

顶了,太有用了,这个技术学名叫什么?学名比如说 继承,方法重载。我得学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马