黑马程序员技术交流社区

标题: 实现文件复制并改名 [打印本页]

作者: 秋天中の夏季    时间: 2015-8-22 23:47
标题: 实现文件复制并改名
import java.io.*;
public class CopyDir {
        public static void main(String[] args) {
                copyDir(new File("c:\\demo"), new File("d:"));
        }
        /*
         * 定义方法,实现文件夹复制
         */
         public static void copyDir(File source,File targer){
                 //使用File对象方法getName获取数据源文件夹名字
                 String sourceDirName = source.getName();
                // System.out.println(sourceDirName);
                 //将数据目的,和数据源文件夹名组成新的File对象
                 File newDirTarget = new File(targer, sourceDirName);
                 newDirTarget.mkdirs();
                 //遍历数据源文件夹
                 File[] sourceFiles = source.listFiles();
                 for(File  f : sourceFiles){
                         //获取出数据源文件名
//                         System.out.println(f.getName());
                         //数据目的文件夹,数据源的文件名,组成新File对象
                         String sourceFileName = f.getName();
                         File newFileTarget = new File(newDirTarget, sourceFileName);
                         //调用copyFile方法,传递File对象
                         copyFile(f,newFileTarget);
                 }
                 //调用修改文件名的方法
                 rename(newDirTarget);
         }
         
         /*
          * 定义方法,实现复制后的文件修改名字
          * File类的方法renameTo(File dest) 修改文件名的功能
          * 调用者File类对象,传递File对象
          *   数据源,       修改后的文件
          *   d:\\demo
          *   listFiles()
          *   d:\\demo\\1.txt  ==> d:\\demo\\1.java
          *   1.txt ==> 1.java
          *   1.docx ==> 1.java
          * 找. 字符串最后一次出现的索引 lastIndexOf(".");=1
          * substring获取.以前的所有内容
          * substring(0,5),加固定的名字.java
          */
         public static void rename(File source){
                 //遍历数据源
                 File[] files = source.listFiles();
                 for(File f : files){
                         //System.out.println(f);
                         //获取数据源文件名
                         String sourceFileName = f.getName();
                         System.out.println(sourceFileName);
                         //使用lastIndexOf获取.最后一次出现索引
                         int index = sourceFileName.lastIndexOf(".");
                         //文件名进行部分截取substring
                         String str = sourceFileName.substring(0, index)+".java";
                         //获取后的文件名+文件夹名字组成File
                         File reNameFile = new File(source, str);
                         f.renameTo(reNameFile);
                 }
         }
         
         
         
         /*
          * 定义方法,实现文件复制
          * IO流对象构造方法,都接收File对象
          * 带异常处理
          */
          public static void copyFile(File source,File target){
                  FileInputStream fis = null;
                  FileOutputStream fos = null;
                  try{
                          fis = new FileInputStream(source);
                          fos = new FileOutputStream(target);
                          byte[] bytes = new byte[1024];
                          int len = 0 ;
                          while((len = fis.read(bytes))!=-1){
                                  fos.write(bytes, 0, len);
                          }
                  }catch(IOException ex){
                          throw new RuntimeException("文件夹复制失败");
                  }finally{
                          try{
                                        if(fos!=null)
                                                fos.close();
                                }catch(IOException ex){
                                        throw new RuntimeException("资源关闭失败");
                                }finally{
                                        try{
                                                if(fis!=null)
                                                        fis.close();
                                        }catch(IOException ex){
                                                throw new RuntimeException("资源关闭失败");
                                        }
                           }
                  }
          }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2