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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mk7 中级黑马   /  2013-6-13 20:34  /  1517 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

想写一个实现文件夹剪切功能的程序,没成功,不知道问题出在哪,麻烦大家帮忙分析分析,谢谢了 。。
  1. public class MoveFile
  2. {

  3. public static void main(String[] args)
  4. {
  5. //确定文件夹的源目录和目的目录
  6. moveFile("e:\\123", "f:\\123");

  7. System.out.println("move success");
  8. }

  9. //实现剪切功能的方法
  10. public static void moveFile(String src, String des)
  11. {
  12. //将源目录和目的目录封装成文件对象
  13. File file1 = new File(src);
  14. File file2 = new File(des);

  15. //如果源目录是一个文件就直接剪切
  16. if (file1.isFile())
  17. {
  18. file1.renameTo(file2);
  19. }

  20. //如果源目录是一个文件夹,则遍历内部文件
  21. else if (file1.isDirectory())
  22. {
  23. File[] files = file1.listFiles();
  24. for (File file : files)
  25. {
  26. //递归调用moveFile方法
  27. moveFile(file.getPath(), des+"\\"+file.getName());
  28. }

  29. }
  30. }

  31. }
复制代码

  1. <P> </P>
复制代码

评分

参与人数 1技术分 +1 收起 理由
Super_Class + 1

查看全部评分

4 个回复

倒序浏览
你下面这段代码有误,renameTo(...)方法并不是剪切文件,而是重命名文件
//如果源目录是一个文件就直接剪切
if (file1.isFile())
{
     file1.renameTo(file2);
}

下面是我实现文件夹剪切功能的一个例子,虽然并不完善,但是也实现了剪切的功能:
  1. package file;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;

  10. /**
  11. * 一个实现文件夹剪切功能的程序
  12. * @author Chu
  13. *
  14. */
  15. public class MoveFile {
  16.         private static String srcDir;
  17.         private static String desDir;
  18.         static int length;
  19.        
  20.         public static void main(String[] args) throws Exception {
  21.                 //确定文件夹的源目录和目的目录
  22.                 srcDir = "D:\\123";
  23.                 desDir = "H:\\111";
  24.                 length = new File(srcDir).getParentFile().getAbsolutePath().length();
  25.                 moveFile(srcDir, desDir);
  26.                
  27.                 System.out.println("move success");
  28.         }
  29.        
  30.         //实现剪切功能的方法
  31.         public static void moveFile(String src, String des) throws Exception {
  32.                 //将源目录和目的目录封装成文件对象
  33.                 File srcFile = new File(src);
  34.                 cut(srcFile, des);
  35.         }
  36.         //剪切文件夹
  37.         private static void cut(File srcFile, String desFile) throws Exception {
  38.                 // TODO Auto-generated method stub
  39.                 //如果源目录是一个文件夹,则遍历内部文件
  40.                 if (srcFile.isDirectory()) {
  41.                         //文件夹路径
  42.                         String srcPath = srcFile.getAbsolutePath();
  43.                         String  desPath = desFile+srcPath.substring(length-1);
  44.                         //desPath = desPath.replaceAll("\\\\", "\\\\\\\\");
  45.                         File f = new File(desPath);
  46.                         f.mkdir();        //创建目录
  47.                        
  48.                         File[] files = srcFile.listFiles();
  49.                         for (File file : files){
  50.                                 //递归调用moveFile方法
  51.                                 moveFile(file.getPath(), desFile);
  52.                         }
  53.                         srcFile.delete();        //删除原文件目录文件夹
  54.                 }else{
  55.                         copyFile(srcFile, desFile);
  56.                 }
  57.         }
  58.         //复制文件
  59.         private static void copyFile(File srcFile, String desFile) throws Exception {
  60.                 // TODO Auto-generated method stub
  61.                 InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFile), "UTF-8") ;
  62.                
  63.                 String srcPath = srcFile.getParentFile().getAbsolutePath();
  64.                 String  desPath = desFile+srcPath.substring(length-1)+"\\"+srcFile.getName();
  65.                
  66.                 desPath = desPath.replaceAll("\\\\", "\\\\\\\\");
  67.                 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(desPath), "UTF-8") ;
  68.                
  69.                 BufferedReader br = new BufferedReader(isr);
  70.                 BufferedWriter bw = new BufferedWriter(osw);
  71.                
  72.                 String line = null;
  73.                 while((line=br.readLine())!=null)
  74.                 {
  75.                         bw.write(line);
  76.                         bw.newLine();        //换行
  77.                         bw.flush();        //刷新缓冲区
  78.                 }
  79.                
  80.                 try {
  81.                         if(bw!=null)
  82.                                 bw.close();        //关闭缓冲区,也就是关闭流
  83.                         if(br!=null)
  84.                                 br.close();        //关闭缓冲区
  85.                 } catch (IOException e) {
  86.                         System.out.println(e.toString());
  87.                 }
  88.                 srcFile.delete();        //复制完后删除源文件
  89.         }
  90. }
复制代码

评分

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

查看全部评分

回复 使用道具 举报
太感谢了
回复 使用道具 举报
楼主您好~帖子长时间未作出回答,我已经将您的帖子改成已解决。如果有问题的话可以私密我哦~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马