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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© .....淡定 中级黑马   /  2013-9-8 16:24  /  1857 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨增坤 于 2013-9-11 17:40 编辑

貌似老毕的视频中没有讲。。现在正在写复制文件夹的代码。。顺便求一份

7 个回复

倒序浏览
so easy的,自己写吧
回复 使用道具 举报
毕姥爷讲的是递归删除文件夹和复制不上一码事吗
回复 使用道具 举报
  1. public class ReadFile {  
  2.   
  3. /**
  4.   * 删除某个文件夹下的所有文件夹和文件
  5.   *
  6.   * @param delpath
  7.   *            String
  8.   * @throws FileNotFoundException
  9.   * @throws IOException
  10.   * @return boolean
  11.   */  
  12. public static boolean deletefile(String delpath) throws Exception {  
  13.   try {  
  14.   
  15.    File file = new File(delpath);  
  16.    // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true  
  17.    if (!file.isDirectory()) {  
  18.     file.delete();  
  19.    } else if (file.isDirectory()) {  
  20.     String[] filelist = file.list();  
  21.     for (int i = 0; i < filelist.length; i++) {  
  22.      File delfile = new File(delpath + "\\" + filelist[i]);  
  23.      if (!delfile.isDirectory()) {  
  24.       delfile.delete();  
  25.       System.out  
  26.         .println(delfile.getAbsolutePath() + "删除文件成功");  
  27.      } else if (delfile.isDirectory()) {  
  28.       deletefile(delpath + "\\" + filelist[i]);  
  29.      }  
  30.     }  
  31.     System.out.println(file.getAbsolutePath()+"删除成功");  
  32.     file.delete();  
  33.    }  
  34.   
  35.   } catch (FileNotFoundException e) {  
  36.    System.out.println("deletefile() Exception:" + e.getMessage());  
  37.   }  
  38.   return true;  
  39. }  
  40.   
  41. /**
  42.   * 输出某个文件夹下的所有文件夹和文件路径
  43.   *
  44.   * @param delpath
  45.   *            String
  46.   * @throws FileNotFoundException
  47.   * @throws IOException
  48.   * @return boolean
  49.   */  
  50. public static boolean readfile(String filepath)  
  51.    throws FileNotFoundException, IOException {  
  52.   try {  
  53.   
  54.    File file = new File(filepath);  
  55.    System.out.println("遍历的路径为:" + file.getAbsolutePath());  
  56.    // 当且仅当此抽象路径名表示的文件存在且 是一个目录时(即文件夹下有子文件时),返回 true  
  57.    if (!file.isDirectory()) {  
  58.     System.out.println("该文件的绝对路径:" + file.getAbsolutePath());  
  59.     System.out.println("名称:" + file.getName());  
  60.    } else if (file.isDirectory()) {  
  61.     // 得到目录中的文件和目录  
  62.     String[] filelist = file.list();  
  63.     if (filelist.length == 0) {  
  64.      System.out.println(file.getAbsolutePath()  
  65.        + "文件夹下,没有子文件夹或文件");  
  66.     } else {  
  67.      System.out  
  68.        .println(file.getAbsolutePath() + "文件夹下,有子文件夹或文件");  
  69.     }  
  70.     for (int i = 0; i < filelist.length; i++) {  
  71.      File readfile = new File(filepath + "\\" + filelist[i]);  
  72.      System.out.println("遍历的路径为:" + readfile.getAbsolutePath());  
  73.      if (!readfile.isDirectory()) {  
  74.       System.out.println("该文件的路径:"  
  75.         + readfile.getAbsolutePath());  
  76.       System.out.println("名称:" + readfile.getName());  
  77.      } else if (readfile.isDirectory()) {  
  78.       System.out.println("-----------递归循环-----------");  
  79.       readfile(filepath + "\\" + filelist[i]);  
  80.      }  
  81.     }  
  82.   
  83.    }  
  84.   
  85.   } catch (FileNotFoundException e) {  
  86.    System.out.println("readfile() Exception:" + e.getMessage());  
  87.   }  
  88.   return true;  
  89. }  
  90.   
  91. public static void main(String[] args) {  
  92.   try {  
  93.    // readfile("D:/file");  
  94.    deletefile("D:/file");  
  95.   } catch (Exception ex) {  
  96.    ex.printStackTrace();  
  97.   }  
  98.   System.out.println("ok");  
  99. }  
  100.   
  101. }  
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
我记得这是一道入学测试题
回复 使用道具 举报
  1. package com.itheima;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.List;

  11. public class Test9 {

  12.         /**
  13.          * 9、 编写一个程序,将d:\java 目录下的所有.java 文件复制到d:\jad 目录下,并将 原来文件的扩展名从.java 改为.jad。
  14.          */
  15.         public static void main(String[] args) {
  16.                 File oldFileDir = new File("d:\\java"); // 源文件文件夹对象
  17.                 File newFileDir = new File("d:\\jad"); // 目标文件夹对象

  18.                 // 所有文件名称路径
  19.                 List<File> javaFileList = new ArrayList<File>();
  20.                 getFiles(oldFileDir, javaFileList);

  21.                 // 所有新文件名称路径
  22.                 List<File> listNewName = new ArrayList<File>();
  23.                 nameSplit(javaFileList, listNewName, newFileDir);

  24.                 // 复制文件
  25.                 copyFileList(javaFileList, listNewName);
  26.         }

  27.         // 复制多个文件
  28.         private static void copyFileList(List<File> oldFileList,
  29.                         List<File> newFileList) {
  30.                 for (int i = 0; i < oldFileList.size(); i++) {
  31.                         // 调用复制文件方法
  32.                         copyFile(oldFileList.get(i), newFileList.get(i));
  33.                 }
  34.         }

  35.         // 复制单个文件
  36.         private static void copyFile(File oldFile, File newFile) {
  37.                 BufferedReader bufr = null;
  38.                 BufferedWriter bufw = null;
  39.                 try {
  40.                         // 因为我要复制的文件编码为本地编码(GBK),防止乱码
  41.                         bufr = new BufferedReader(new InputStreamReader(
  42.                                         new FileInputStream(oldFile), "GBK"));
  43.                         bufw = new BufferedWriter(new FileWriter(newFile));

  44.                         String line = null;
  45.                         while ((line = bufr.readLine()) != null) {
  46.                                 bufw.write(line);
  47.                                 bufw.newLine();
  48.                                 bufw.flush();
  49.                         }
  50.                 } catch (Exception e) {
  51.                         System.out.println(e.toString());
  52.                 } finally {
  53.                         try {
  54.                                 if (bufr != null)
  55.                                         bufr.close();
  56.                         } catch (IOException e) {
  57.                                 System.out.println(e.toString());
  58.                         }

  59.                         try {
  60.                                 if (bufw != null)
  61.                                         bufw.close();
  62.                         } catch (IOException e) {
  63.                                 System.out.println(e.toString());
  64.                         }
  65.                 }

  66.         }

  67.         // 获取新生成文件路径名称
  68.         private static void nameSplit(List<File> files, List<File> listNewName,
  69.                         File newFileDir) {
  70.                 String ss = null;
  71.                 for (int i = 0; i < files.size(); i++) {
  72.                         String s = files.get(i).getName();
  73.                         // 截取.java前的文件名称
  74.                         String[] arrs = s.split("\\.");
  75.                         // 拼接字符串生成新文件名称
  76.                         ss = arrs[0] + ".jad";
  77.                         // 装成File对象,添加到新名称集合中封
  78.                         listNewName.add(new File(newFileDir, ss));
  79.                 }
  80.         }

  81.         // 该方法用来获取所有文件夹下所有的文件
  82.         private static void getFiles(File oldFileDir, List<File> javaFileList) {

  83.                 File[] fileDirs = oldFileDir.listFiles();
  84.                 for (int i = 0; i < fileDirs.length; i++) {
  85.                         // 如果是目录递归调用该方法获取所有文件
  86.                         if (fileDirs[i].isDirectory()) {
  87.                                 getFiles(fileDirs[i], javaFileList);
  88.                         } else {
  89.                                 // 判断文件是否以.java结尾,如果拓展名不是.java则不会被添加到集合中
  90.                                 if (fileDirs[i].getName().endsWith(".java"))
  91.                                         javaFileList.add(fileDirs[i]);
  92.                         }
  93.                 }
  94.         }

  95. }
复制代码
改吧改吧用吧~!

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 gudao20080 于 2013-9-9 15:30 编辑

正好上午做了这个练习,你参考看看
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. public class Test1 {

  8. /**
  9. * @param args
  10. * 复制文件夹
  11. * @throws IOException
  12. */
  13. public static void main(String[] args) throws IOException {
  14. File src = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.22.02");
  15. File des = new File("D:\\360安全浏览器下载") ;


  16. copyDir(src, des);
  17. //copyFile(src, des);

  18. }
  19. //复制文件夹
  20. public static void copyDir(File src, File des) throws IOException{

  21. if(!des.exists()){ //目录不存在,则新建目录
  22. des.mkdir();
  23. }

  24. File[] files = src.listFiles();
  25. for(File file: files){
  26. String fileName = file.getName(); //文件名
  27. System.out.println(fileName);
  28. File desFile = new File(des.getAbsolutePath()+"\\"+fileName); //目标文件
  29. if(!file.isDirectory()){

  30. BufferedInputStream buis = new BufferedInputStream(new FileInputStream(file));
  31. BufferedOutputStream buos = new BufferedOutputStream(new FileOutputStream(desFile));
  32. byte[] bys = new byte[1024];
  33. int len = 0;
  34. while((len = buis.read(bys)) != -1){
  35. buos.write(bys, 0, len);
  36. }

  37. buis.close();
  38. buos.close();

  39. }else{
  40. desFile.mkdir();
  41. copyDir(file, desFile); //如果是文件夹,递归调用
  42. }

  43. }
  44. }
  45. //复制文件
  46. public static void copyFile(File src, File des) throws IOException{
  47. String fileName = src.getName(); //源文件名
  48. BufferedInputStream buis = new BufferedInputStream(new FileInputStream(src));
  49. String desPath = des.getAbsolutePath()+"\\"+fileName; //目标文件绝对路径
  50. File desFile = new File(desPath); //目标文件
  51. BufferedOutputStream buos = new BufferedOutputStream(new FileOutputStream(desFile));

  52. byte[] bys = new byte[1024];
  53. int len = 0;
  54. while((len = buis.read(bys)) != -1){
  55. buos.write(bys, 0, len);
  56. }

  57. buis.close();
  58. buos.close();
  59. }

  60. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
楼主,你好,如果您的问题解决了!


请把您问题的未解决更改为已解决

谢谢合作!


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马