`package ssh;java
import com.xxx.common.log.LogFactory; import com.xxx.common.log.LoggerUtil; import org.apache.commons.lang3.StringUtils;apache
import java.io.*;app
public class DirErgodic {ssh
private static final LoggerUtil logger = LogFactory.getLogger(DirErgodic.class); private static int depth = 1; public static void find(String pathName, int depth) throws IOException { int filecount = 0; //獲取pathName的File對象 File dirFile = new File(pathName); //判斷該文件或目錄是否存在,不存在時在控制檯輸出提醒 if (!dirFile.exists()) { System.out.println("do not exit"); return; } //判斷若是不是一個目錄,就判斷是否是一個文件,時文件則輸出文件路徑 if (!dirFile.isDirectory()) { if (dirFile.isFile()) { //System.out.println(dirFile.getCanonicalFile()); } return; } for (int j = 0; j < depth; j++) { //System.out.print(" "); } //System.out.print("|--"); //System.out.println(dirFile.getName()); //獲取此目錄下的全部文件名與目錄名 String[] fileList = dirFile.list(); int currentDepth = depth + 1; for (int i = 0; i < fileList.length; i++) { //遍歷文件目錄 String string = fileList[i]; //File("documentName","fileName")是File的另外一個構造器 File file = new File(dirFile.getPath(), string); String name = file.getName(); //若是是一個目錄,搜索深度depth++,輸出目錄名後,進行遞歸 if (file.isDirectory()) { //遞歸 find(file.getCanonicalPath(), currentDepth); } else { //若是是文件,則直接輸出文件名 for (int j = 0; j < currentDepth; j++) { //System.out.print(" "); } //只須要java文件 if (StringUtils.endsWith(name, ".java")) { //System.out.print("|--"); //System.out.println(name); replacTextContent(file.getPath()); } } } } /** * 替換文本文件中的 非法字符串 * * [@param](https://my.oschina.net/u/2303379) path * [@throws](https://my.oschina.net/throws) IOException */ public static void replacTextContent(String path) throws IOException { logger.info("正在讀取 path:" + path); // 讀 File file = new File(path); FileReader in = new FileReader(file); BufferedReader bufIn = new BufferedReader(in); // 內存流, 做爲臨時流 CharArrayWriter tempStream = new CharArrayWriter(); // 替換 String line = null; String fir = null, sec = null; boolean needChange = false; boolean noFirWrite = false; boolean alreadyChanged = false; while ((line = bufIn.readLine()) != null) { //方法及以後 if (StringUtils.isNotBlank(fir) && StringUtils.isNotBlank(sec)) { if (!needChange) { //獲取方法 needChange = true; //裝裝代碼 String func = line; if (StringUtils.contains(func, "/")) { alreadyChanged = true; } addLine(tempStream, func, noFirWrite); addLine(tempStream, fir, noFirWrite); addLine(tempStream, sec, noFirWrite); } else { //後面的代碼 addLine(tempStream, line, noFirWrite); } } //日期 if (StringUtils.isNotBlank(fir)) { if (StringUtils.contains(line, "@date")) { //獲取日期 sec = line; } } //做者 if (StringUtils.contains(line, "@author")) { //獲取做者 fir = line; } if (StringUtils.isBlank(fir)) { //第一次前面的代碼 addLine(tempStream, line, noFirWrite); } noFirWrite = true; } if (needChange && !alreadyChanged) { //先方法在做者在日期 //組裝三個部分代碼 FileWriter out = new FileWriter(file); tempStream.writeTo(out); out.close(); logger.debug("正在修改 path:" + path); } // 關閉 輸入流 bufIn.close(); } private static void addLine(CharArrayWriter tempStream, String line, boolean noFirWrite) throws IOException { if (noFirWrite) { // 添加換行符 tempStream.append(System.getProperty("line.separator")); } // 將該行寫入內存 tempStream.write(line); } public static void main(String[] args) throws IOException { find("E:\\xxx", depth); //replacTextContent("E:\\xxx\\xxx-service\\src\\main\\java\\com\\xxx\\xxx\\rpc\\dao\\data\\xxx.java"); }
} `.net