/** * 文件后缀名修改器 * 说明:UI部分 * @author Yours风之恋 * * 创建时间:2012-8-8 * */public class UserInterface { public static void mainMenuDisplay() { System.out.println("-------------------批量修改文件后缀名----------------------"); System.out.println(" Copyright 2012 "); System.out.println(" @author Yours风之恋 "); System.out.println("-----------------------------------------------------------\n"); } public static void helpMenuDisplay() { System.out.println("-------------------系统帮助-------------------"); System.out.println("本系统需要两个参数:"); System.out.println("1,需要修改的文件所在文件夹目录"); System.out.println("2,自定义的后缀名"); } public static void waitUserInput() { System.out.println(); System.out.println("系统正在等待您的输入:"); }}
import java.io.File;import java.util.Scanner;public class FEA { public static void main(String[] args) { init(); start(); } public static void start() { System.out.println("请输入文件所在文件夹目录,按回车键结束。"); String dir = getInput(); list(dir, 0); System.out.println("请输入自定义的后缀名,按回车键结束。"); String extension = getInput(); long l1 = System.currentTimeMillis(); change(dir, extension); long l2 = System.currentTimeMillis(); System.out.println("后缀名修改成功,用时:" + (l2-l1) + "毫秒"); } public static void listFiles(File f, int level, int key) { if (!f.isDirectory()) { System.out.println("目录不正确,本次服务结束 !"); System.exit(0); } String preStr = ""; for (int i = 0; i < level; i++) { preStr += " "; } File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) { System.out.println(preStr + childs[i].getName()); if (childs[i].isDirectory() && key == 0) { listFiles(childs[i], level + 1, key); } } } public static void changeExtension(File f, String extension) { if (!f.isDirectory()) { System.out.println("目录不正确,本次服务结束 !"); System.exit(0); } File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) { if (childs[i].isFile()) { String parStr = childs[i].getParent(); String filStr = childs[i].getName(); StringBuilder sbStr = new StringBuilder(filStr); for(int k = sbStr.length() -1; k >= 0 ; k--) { char c = sbStr.charAt(k); if(c != '.') { sbStr.deleteCharAt(k); } else { break; } } sbStr.append(extension); File fx = new File(parStr, sbStr.toString()); childs[i].renameTo(fx); } if (childs[i].isDirectory()) { changeExtension(childs[i], extension); } } } public static void change(String dir, String extension) { File f = new File(dir); changeExtension(f, extension); } public static void list(String dir, int key) { File f = new File(dir); listFiles(f, 0, key); } public static String getInput() { Scanner sc = new Scanner(System.in); return sc.next(); } public static void init() { UserInterface.mainMenuDisplay(); UserInterface.helpMenuDisplay(); UserInterface.waitUserInput(); }}
另有源码和可执行文件: