使用java来把一个目录下的所有文件拷贝到另外一个目录下,并且重命名

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.File;  
  2. import java.io.FileReader;  
  3. import java.io.FileWriter;  
  4.   
  5.   
  6. public class HandleFilename {  
  7.   
  8.     public static String destDir = "D:\\MyCopyFile";  
  9.       
  10.     public static void main(String[] args) throws Exception{  
  11.           
  12.         createDestDir(destDir);  
  13.           
  14.         String pathname = "D:\\WDJDownload\\Apps\\Hair Salon\\assets";  
  15.         File file = new File(pathname);  
  16.         handleFile(file);  
  17.           
  18.           
  19.     }  
  20.       
  21.     /** 
  22.      * 创建一个新的目录 
  23.      * @param dirName 
  24.      */  
  25.     public static void createDestDir(String dirName){  
  26.         File file = new File( dirName);  
  27.         if(!file.exists()){  
  28.             file.mkdir();  
  29.         }  
  30.     }  
  31.       
  32.     /** 
  33.      * 遍历每一个文件... 
  34.      * @param file 
  35.      * @throws Exception 
  36.      */  
  37.     public static void handleFile(File file) throws Exception{  
  38.         File[] files = file.listFiles();  
  39.         System.out.println("------------> files.length: " + files.length);  
  40.         for(File f : files){  
  41.             if(f.isDirectory()){  
  42.                 handleFile(f);  
  43.             }else{  
  44.                 /** 
  45.                  * file.canWrite():判断文件是否可写.... 
  46.                  */  
  47. //              System.out.println("--------> file.canWrite()" + file.canWrite());  
  48. //              System.out.println("--------> file.canRead()" + file.canRead());  
  49. //              System.out.println("--------> file.canExecute()" + file.canExecute());  
  50.                 System.out.println( "-------------->" + f.getName());  
  51.                 copyFile(f);  
  52.             }  
  53.         }  
  54.     }  
  55.       
  56.     /** 
  57.      * 复制一个文件 
  58.      * @param file 
  59.      * @throws Exception 
  60.      */  
  61.     public static void copyFile(File file) throws Exception{  
  62.           
  63.         String oldFileName = file.getName();  
  64.         if(oldFileName.endsWith("_jpg")){//修改后缀名...  
  65.             oldFileName = oldFileName.replace("_jpg"".jpg");  
  66.         }else if(oldFileName.endsWith("_png")){  
  67.             oldFileName = oldFileName.replace("_png"".png");  
  68.         }  
  69.           
  70.         String newFileName = oldFileName;  
  71.         /** 
  72.          * file.getAbsolutePath(): 返回带文件名的绝对路径... 
  73.          */  
  74.         System.out.println("----------->file.getAbsolutePath(): " + file.getAbsolutePath());  
  75.         System.out.println("----------->file.getName(): " + file.getName());  
  76.           
  77.         FileReader reader = new FileReader(file.getAbsolutePath() );  
  78.         FileWriter writer = new FileWriter(destDir + "\\" + newFileName);  
  79.           
  80.         int ch;  
  81.         while((ch = reader.read()) != -1){  
  82.             writer.write(ch);  
  83.         }  
  84.           
  85.         reader.close();  
  86.         writer.close();  
  87.     }  
  88. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值