代码注释中顺序更改 文件读写换行

`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

相关文章
相关标签/搜索