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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© micro_hx 中级黑马   /  2015-6-21 23:55  /  435 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

试题:从一个盘符拷贝文件夹到另一个盘符,文件夹里需要有嵌套文件夹。(其中内部存在很多大文件,注意复制效率)  题目可以有很多思路,本屌丝面试写的一种方法,现在奉上:
  1. package com.itheima;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.Map.Entry;

  12. /**
  13. * 完全复制某个文件夹下所有内容
  14. * 将D:\\test复制到 E:\\test中
  15. * @author Administrator
  16. */
  17. public class Test3 {

  18.         //String为文件路劲,Boolean为文件夹还是文件
  19.         static Map<String,Boolean> container  = new HashMap<String,Boolean>();         

  20.         public static void main(String[] args) {
  21.                 listFile("D:\\test");
  22.                 copyFile();
  23.         }

  24.         /**
  25.          * 罗列文件名称
  26.          * @param filePath
  27.          */
  28.         static void listFile(String filePath){
  29.                 File parentFile = new File(filePath);
  30.                 File[] files = parentFile.listFiles() ;
  31.                 if(files != null && files.length > 0){
  32.                         for(File file : files){
  33.                                 String fileName  =file.getAbsolutePath() ;
  34.                                 if(file.isDirectory()){
  35.                                         container.put(fileName, true);
  36.                                         listFile(fileName);
  37.                                 }else{
  38.                                         container.put(fileName, false);
  39.                                 }
  40.                                 System.out.println(file.getAbsolutePath());
  41.                         }
  42.                 }
  43.         }
  44.                
  45.                 static void copyFile(){
  46.                         Iterator<Entry<String,Boolean>>  iterator = container.entrySet().iterator() ;
  47.                         BufferedInputStream input = null ;
  48.                         BufferedOutputStream output = null ;
  49.                        
  50.                         while(iterator.hasNext()){
  51.                                 Entry<String,Boolean> entry = iterator.next();
  52.                                 String filePath = entry.getKey() ;
  53.                                 boolean isDir = entry.getValue() ;
  54.                                 if(filePath != null){
  55.                                         String copyPath = "E:"+filePath.substring(2);
  56.                                         if(isDir){
  57.                                                 File newDir = new File(copyPath);
  58.                                                 if(!newDir.exists())
  59.                                                         newDir.mkdirs() ;
  60.                                         }else{
  61.                                                 String dirPath = copyPath.substring(0, copyPath.lastIndexOf("\\")+1        ) ;
  62.                                                 File newDir = new File(dirPath);
  63.                                                 if(!newDir.exists())
  64.                                                         newDir.mkdirs();
  65.                                                 File newFile = new File(copyPath);
  66.                                                 if(!newFile.exists()){
  67.                                                         try {
  68.                                                                 newFile.createNewFile();
  69.                                                                
  70.                                                                 //写入文件
  71.                                                                 input = new BufferedInputStream(new FileInputStream(new File(filePath)));
  72.                                                                 output = new BufferedOutputStream(new FileOutputStream(newFile));

  73.                                                                 byte[] buf = new byte[1024*50];
  74.                                                                 int len = 0 ;
  75.                                                                
  76.                                                                 while((len = input.read(buf)) != -1){
  77.                                                                         output.write(buf, 0, len);
  78.                                                                         output.flush();
  79.                                                                 }
  80.                                                                
  81.                                                         } catch (IOException e) {
  82.                                                                 e.printStackTrace();
  83.                                                         }finally{
  84.                                                                 if(input != null){
  85.                                                                         try {
  86.                                                                                 input.close();
  87.                                                                         } catch (IOException e) {
  88.                                                                                 e.printStackTrace();
  89.                                                                         }
  90.                                                                 }
  91.                                                                
  92.                                                                 if(output != null){
  93.                                                                         try {
  94.                                                                                 output.close();
  95.                                                                         } catch (IOException e) {
  96.                                                                                 e.printStackTrace();
  97.                                                                         }
  98.                                                                 }
  99.                                                         }
  100.                                                 }
  101.                                 }
  102.                         }
  103.                 }
  104.         }
  105. }
复制代码
看看大家思路啊。。。





0 个回复

您需要登录后才可以回帖 登录 | 加入黑马