今天要给一大堆文件换扩展名.网上找了半天没找到合适的工具.就用java写了一个.写的太丑陋了.都不想再看.以后有时间了好好另写个.
//改目录下文件名的扩展名 import java.io.File; public class re { public static void main(String[] args){ File file = new File("D:\\tmep"); String pre = new String("sql"); String aft = new String("txt"); run(file,pre,aft); } public static void run(File f,String from,String to) { if (f.isFile()) renname(f,from,to); else for (File a:f.listFiles()) run(a,from,to); } public static String getSubname(File f) { String[] names = f.getName().split("\\."); if (names.length>1) return names[names.length-1]; else return ""; } public static String getname(File f ) { String[] names = f.getName().split("\\."); String filename = names[0]; for(int i=1;i<names.length-1;i++ ) { filename = filename "." names[i]; } return filename; } public static void renname(File renf,String from,String to) { if (renf.isFile()&&(getSubname(renf).equalsIgnoreCase(from))) { renf.renameTo(new File(getname(renf)+ "."+ to)); System.out.println(renf.getPath()+ "->"+ getname(renf)+ "."+ to); } } }