java例程练习(批量修改文件后缀名)

  1. /** 
  2.  * 文件后缀名修改器 
  3.  * 说明:UI部分 
  4.  * @author Yours风之恋 
  5.  *  
  6.  * 创建时间:2012-8-8 
  7.  * 
  8.  */  
  9. public class UserInterface {  
  10.     public static void mainMenuDisplay() {  
  11.         System.out.println("-------------------批量修改文件后缀名----------------------");  
  12.         System.out.println("  Copyright 2012              ");  
  13.         System.out.println("  @author Yours风之恋                     ");  
  14.         System.out.println("-----------------------------------------------------------\n");  
  15.     }  
  16.       
  17.     public static void helpMenuDisplay() {  
  18.         System.out.println("-------------------系统帮助-------------------");  
  19.         System.out.println("本系统需要两个参数:");  
  20.         System.out.println("1,需要修改的文件所在文件夹目录");  
  21.         System.out.println("2,自定义的后缀名");  
  22.     }  
  23.       
  24.     public static void waitUserInput() {  
  25.         System.out.println();  
  26.         System.out.println("系统正在等待您的输入:");  
  27.     }  
  28. }  
  1. import java.io.File;  
  2. import java.util.Scanner;  
  3.   
  4. public class FEA {  
  5.   
  6.     public static void main(String[] args) {  
  7.         init();  
  8.         start();  
  9.     }  
  10.       
  11.     public static void start() {  
  12.         System.out.println("请输入文件所在文件夹目录,按回车键结束。");  
  13.         String dir = getInput();  
  14.         list(dir, 0);  
  15.         System.out.println("请输入自定义的后缀名,按回车键结束。");  
  16.         String extension = getInput();  
  17.         long l1 = System.currentTimeMillis();  
  18.         change(dir, extension);  
  19.         long l2 = System.currentTimeMillis();  
  20.         System.out.println("后缀名修改成功,用时:" + (l2-l1) + "毫秒");  
  21.     }  
  22.   
  23.     public static void listFiles(File f, int level, int key) {  
  24.         if (!f.isDirectory()) {  
  25.             System.out.println("目录不正确,本次服务结束 !");  
  26.             System.exit(0);  
  27.         }  
  28.           
  29.         String preStr = "";  
  30.         for (int i = 0; i < level; i++) {  
  31.             preStr += "    ";  
  32.         }  
  33.   
  34.         File[] childs = f.listFiles();  
  35.         for (int i = 0; i < childs.length; i++) {  
  36.             System.out.println(preStr + childs[i].getName());  
  37.   
  38.             if (childs[i].isDirectory() && key == 0) {  
  39.                 listFiles(childs[i], level + 1, key);  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44.     public static void changeExtension(File f, String extension) {  
  45.         if (!f.isDirectory()) {  
  46.             System.out.println("目录不正确,本次服务结束 !");  
  47.             System.exit(0);  
  48.         }  
  49.   
  50.         File[] childs = f.listFiles();  
  51.         for (int i = 0; i < childs.length; i++) {  
  52.             if (childs[i].isFile()) {  
  53.                 String parStr = childs[i].getParent();  
  54.   
  55.                 String filStr = childs[i].getName();  
  56.                 StringBuilder sbStr = new StringBuilder(filStr);  
  57.                   
  58.                 for(int k = sbStr.length() -1; k >= 0 ; k--) {  
  59.                     char c = sbStr.charAt(k);  
  60.                     if(c != '.') {  
  61.                         sbStr.deleteCharAt(k);  
  62.                     } else {  
  63.                         break;  
  64.                     }  
  65.                 }  
  66.                 sbStr.append(extension);  
  67.                 File fx = new File(parStr, sbStr.toString());  
  68.                 childs[i].renameTo(fx);  
  69.                   
  70.             }  
  71.   
  72.             if (childs[i].isDirectory()) {  
  73.                 changeExtension(childs[i], extension);  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78.     public static void change(String dir, String extension) {  
  79.         File f = new File(dir);  
  80.         changeExtension(f, extension);  
  81.     }  
  82.   
  83.     public static void list(String dir, int key) {  
  84.         File f = new File(dir);  
  85.         listFiles(f, 0, key);  
  86.     }  
  87.   
  88.     public static String getInput() {  
  89.         Scanner sc = new Scanner(System.in);  
  90.         return sc.next();  
  91.     }  
  92.   
  93.     public static void init() {  
  94.         UserInterface.mainMenuDisplay();  
  95.         UserInterface.helpMenuDisplay();  
  96.         UserInterface.waitUserInput();  
  97.     }  
  98. }  

另有源码和可执行文件:http://download.csdn.net/detail/haifengzhilian/4494395


转载来自:http://blog.csdn.net/haifengzhilian/article/details/7857608

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值