由于项目需要,需要对一个文件夹下面的所有文件进行替换操作,于是自己动手写了个,里面用到了递归算法,但是程序存在BUG,所以今天先把现在的拿出来大家分享,然后希望大家一起动手把程序完善,谢谢
import java.util.*; import java.io.*; public class Test { // 全局变量,用于存放所有的地址 private static ArrayList arrayList = new ArrayList(); // 全局变量,用于存放随时改变的地址 private static String address = new String(); public static void main(String[]args) { try { Test test = new Test(); address = new String(args[0]); File allFile = new File(address); StringBuffer temp = new StringBuffer(); if (allFile.exists()) { if (!allFile.isDirectory()) { System.out.println( "Destination Path is error(FileOperate.copyFiles())"); return ; } else { ArrayList list = getInfo(allFile); System.out.println("the length of list is " + list.size()); String[]temp_middle; for (int i = 0; i < list.size(); i++) { temp_middle = (String[])list.get(i); for (int j = 0; j < temp_middle.length; j++) { //System.out.println("address is "+temp[j]); File file = new File(address + "//" + temp_middle[j]); if (!file.isDirectory()) { temp = test.readAllFile(address + "//" + temp_middle[j]); System.out.println(temp.toString() + "end"); String result = test.replaceFileString(temp.toString(), args[1], args[2]); System.out.println("result " + result); test.writeFile(address + "//", temp_middle[j], result); } } System.out.println("is the end of " + i); } } } } catch (Exception e) { System.out.println(e); } } /** * 文件的读取 * @param filePath * @param fileName * @throws IOException */ public StringBuffer readAllFile(String filePath)throws IOException { ArrayList arrayList = new ArrayList(); FileReader fr = new FileReader(filePath); int count = fr.read(); arrayList.add(String.valueOf((char)count)); while (count != - 1) { System.out.print((char)count); count = fr.read(); arrayList.add(String.valueOf((char)count)); } fr.close(); return arrayToStringBuffer(arrayList); } /** * ArrayList转换为StringBuffer * * @param arrayList */ public StringBuffer arrayToStringBuffer(ArrayList arrayList) { StringBuffer returnValue = new StringBuffer(); String temp = new String(); for (int i = 0; i < arrayList.size(); i++) { temp = (String)arrayList.get(i); returnValue.append(temp); } return returnValue; } /** * 文件的写入 * @param filePath * @param fileName * @param args * @throws IOException */ public void writeFile(String filePath, String fileName, String args)throws IOException { FileWriter fw = new FileWriter(filePath + fileName); PrintWriter out = new PrintWriter(fw); //for (int i = 0; i < args.length; i++) //{ out.write(args); out.println(); out.flush(); // } fw.close(); out.close(); } /** * 文件内容的替换 * @param all * @param oldString * @param newString */ public String replaceFileString(String all, String oldString, String newString) { String result = new String(); result = all.toString().replaceAll(oldString, newString); return result; } /** * 参数的正确性的check * @param args[0] * @param args[1] * @param args[2] * @param args[3] */ public boolean argsCheck(String args0, String args1, String args2, String args3) { if (args0.equals("")) { System.out.println("args[0] is blank"); return false; } if (args1.equals("")) { System.out.println("args[1] is blank"); return false; } if (args2.equals("")) { System.out.println("args[2] is blank"); return false; } if (args3.equals("")) { System.out.println("args[3] is blank"); return false; } return true; } /** * 递归取得文件夹内部的所有文件地址 * @param allFile */ public static ArrayList getInfo(File allFile) { String[]fileName = allFile.list(); arrayList.add(fileName); for (int i = 0; i < fileName.length; i++) { File tempFile = new File(address + "//" + fileName[i]); System.out.println("the file address is " + tempFile.getPath()); //File x= new File("c://test//musci"); if (tempFile.isDirectory()) { System.out.println("the tempFile address is " + tempFile.getPath()); int length = address.length(); address += "//" + fileName[i]; getInfo(tempFile); address = address.substring(0, length); System.out.println("substring is " + address); } System.out.println("ADDRESS " + i + " " + fileName[i]); } return arrayList; } }