日期:2014-05-20 浏览次数:20775 次
import java.io.*; import java.util.ArrayList; import java.util.List; import java.awt.*; public class Test6 { public static void main(String[] args) { File f = new File("I:\\11"); renameFile(f); } public static void renameFile(File f) { int n = f.listFiles().length; List<String> files = new ArrayList<String>(); try { for (int i = 0; i < f.listFiles().length; i++) { if (f.listFiles().length != 0) { if (!(f.listFiles()[i].isFile())) { files.add(f.getPath() + "\\" + f.listFiles()[i].getName()); renameFile(f.listFiles()[i]); } }else { files.add(f.getPath() + "\\" + f.getName()); } } } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i <files.size() ; i++) { //System.out.println(files.get(i)); File ff = new File(files.get(i)); //String sNoSpace = files.get(i).replaceAll(" ", ""); String sNoSpace = mergeSpace(files.get(i)); System.out.println(sNoSpace); ff.renameTo(new File(sNoSpace)); } } public static String mergeSpace(String s) { StringBuffer strBuf = new StringBuffer(); String[] arrStr = s.split(" "); for (int i = 0; i < arrStr.length; i++) { if (!arrStr[i].equals(" ")) { strBuf.append(arrStr[i]); } } return strBuf.toString(); } }
import java.io.*; import java.util.*; public class tmp1{ public static List<String> files=new ArrayList<String>(); public static void renameFiles(String f){ File ff=new File(f); if(!ff.isDirectory()) {files.add(ff.toString());return;} String[] fl=ff.list(); for(String s:fl){ renameFiles(f+File.separator+s); } } public static String mergeSpace(String s) { StringBuffer strBuf = new StringBuffer(); String[] arrStr = s.split(" "); for (int i = 0; i < arrStr.length; i++) { if (!arrStr[i].equals(" ")) { strBuf.append(arrStr[i]); } } return strBuf.toString(); } public static void main(String[] args){ renameFiles("I:\\11"); for(String ss:files){ String fs=mergeSpace(ss); System.out.println(fs); } } }