自己做的一个Check文件内容是否一致的小工具

 

GetAllFiles.java  (获取指定文件夹中的文件,并检查内容是否一致)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class GetAllFiles {

	// 文件格式
	private static List<CsvInfo> listCsvInfo = new ArrayList<CsvInfo>();

	public static void main(String[] args) {

		// 路径 这里写一个路径进去
		String path = "D:\\WEB-INF\\view";

		// 调用方法
		// getFiles(path);
		getFiles(path, ".jsp");

		StringBuffer content = new StringBuffer();
		String temp = "";
		for (int i = 0; i < listCsvInfo.size(); i++) {
			temp = String.valueOf(i + 1) + "," + listCsvInfo.get(i).getFileName() + ",Line:"
					+ listCsvInfo.get(i).getLineNumber() + "," + listCsvInfo.get(i).getHeaderParam() + ","
					+ listCsvInfo.get(i).getFormAction() + "," + listCsvInfo.get(i).getResult() + ","
					+ listCsvInfo.get(i).getFilePath();
			System.out.println(temp);
			content.append(temp + "\r\n");
		}

		FileUtil.writeUtf8File("d:\\test1.csv", content.toString());
	}

	/**
	 * 
	 * 递归获取某路径下的所有文件,文件夹,并输出
	 * 
	 */
	public static void getFiles(String path) {
		File file = new File(path);
		// 如果这个路径是文件夹
		if (file.isDirectory()) {
			// 获取路径下的所有文件
			File[] files = file.listFiles();

			for (int i = 0; i < files.length; i++) {
				// 如果还是文件夹 递归获取里面的文件 文件夹
				if (files[i].isDirectory()) {
					System.out.println("目录:" + files[i].getPath());
					getFiles(files[i].getPath());
				} else {
					System.out.println("文件:" + files[i].getPath());
				}

			}
		} else {
			System.out.println("文件:" + file.getPath());
		}
	}

	/**
	 * 
	 * 递归获取某路径下的所有文件,文件夹,并输出
	 * 
	 */
	public static void getFiles(String path, String fileType) {
		File file = new File(path);
		// 如果这个路径是文件夹
		if (file.isDirectory()) {
			// 获取路径下的所有文件
			File[] files = file.listFiles();

			for (int i = 0; i < files.length; i++) {
				// 如果还是文件夹 递归获取里面的文件 文件夹
				if (files[i].isDirectory()) {
					// System.out.println("目录:" + files[i].getPath());
					getFiles(files[i].getPath(), fileType);
				} else {
					if (files[i].getName().endsWith(".jsp")) {
						// System.out.println("jsp文件:" + files[i].getPath());
						readTxtFile(files[i].getPath());
					}
				}

			}
		} else {
			if (file.getName().endsWith(".jsp")) {
				System.out.println("jsp文件:" + file.getPath());
			}
		}

	}

	/**
	 * 功能:Java读取txt文件的内容 步骤:1:先获得文件句柄 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
	 * 3:读取到输入流后,需要读取生成字节流 4:一行一行的输出。readline()。 备注:需要考虑的是异常情况
	 * 
	 * @param filePath
	 */
	public static void readTxtFile(String filePath) {
		try {
			String encoding = "UTF-8";
			File file = new File(filePath);

			boolean pickupStartFlg = false;
			boolean pickupEndFlg = false;
			CsvInfo csvInfo = new CsvInfo();
			int lineNumber = 0;
			int strarLine = 0;

			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;

				while ((lineTxt = bufferedReader.readLine()) != null) {
					lineNumber++;
					// System.out.println(lineTxt);

					// 取得header文件的 value字符串
					if (checkString(lineTxt, "page=\"../../common/headerNew.jsp\"")) {
						pickupStartFlg = true;
						strarLine = lineNumber;
					}

					if (pickupStartFlg && (strarLine + 3) < lineNumber && checkString(lineTxt, "</jsp:include>")) {
						pickupEndFlg = true;
					}

					if (pickupStartFlg == true && pickupEndFlg == false) {
						if (checkString(lineTxt, "<jsp:param name=\"HeaderParam\"")) {
							String headerParam = lineTxt.replaceAll("<jsp:param name=\"HeaderParam\" value=\"", "");
							headerParam = headerParam.replaceAll("\" />", "");
							headerParam = headerParam.replaceAll("\t", "").trim();

							csvInfo.setFileName(file.getName());
							csvInfo.setFilePath(filePath);
							csvInfo.setHeaderParam(headerParam);
							csvInfo.setLineNumber(lineNumber);

							listCsvInfo.add(csvInfo);
							pickupEndFlg = true;
						}
					}

					// 取得formAction的.do字符串
					if (pickupStartFlg == true && pickupEndFlg == true) {
						if (checkString(lineTxt, "action=\"${pageContext.request.contextPath}")) {
							int start = lineTxt.indexOf("/") + 1;
							int end = 0;
							String formAction = lineTxt.substring(start);
							end = formAction.indexOf(".do");
							formAction = formAction.substring(0, end);
							csvInfo.setFormAction(formAction);
						}
					}

					// Check是否相等,相等OK, 不相等NG
					if (csvInfo.getHeaderParam().equals(csvInfo.getFormAction())) {
						csvInfo.setResult("OK");
					} else {
						csvInfo.setResult("NG");
					}
				}

				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}

		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}

	}

	private static boolean checkString(String strSend, String searchStr) {

		if (strSend.indexOf(searchStr) > 0) {
			return true;
		} else {
			return false;
		}
	}

}

  

FileUtil.java  (文件的读写)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

	public static void main(String[] args) throws Exception {
		List<String> fileNames = new ArrayList<String>();
		FileUtil.findFileList(new File("F:\\HTML\\bootstrap"), fileNames);
		System.out.println();
		for (String value : fileNames) {
			System.out.println("file:" + value);
		}

	}

	/**
	 * 读取目录下的所有文件
	 * 
	 * @param dir       目录
	 * @param fileNames 保存文件名的集合
	 * @return
	 */
	public static void findFileList(File dir, List<String> fileNames) {
		if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
			return;
		}
		String[] files = dir.list();// 读取目录下的所有目录文件信息
		for (int i = 0; i < files.length; i++) {// 循环,添加文件名或回调自身
			File file = new File(dir, files[i]);
			if (file.isFile()) {// 如果文件
				fileNames.add(dir + "\\" + file.getName());// 添加文件全路径名
			} else {// 如果是目录
				findFileList(file, fileNames);// 回调自身继续查询
			}
		}
	}
	
	// "test.txt"
	public static void writeUtf8File(String fileName, String fileContent) {
		// java中文件操作体现了设计模式中的装饰者模式 。
		// 以UTF-8编码写入文件:
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos = new FileOutputStream(fileName);
			osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(fileContent);
			osw.flush();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static void readUtf8File(String fileName) {
		// 以UTF-8编码读取文件:
		FileInputStream fis = null;
		InputStreamReader isr = null;
		String fileContent = "";

		try {
			fis = new FileInputStream(fileName);
			isr = new InputStreamReader(fis, "UTF-8");
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				fileContent += line;
				fileContent += "\r\n"; // 补上换行符
			}
			System.out.println(fileContent);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

  

CsvInfo.java  (文件内容的实体类)

public class CsvInfo {

	private String fileName = "";
	private String headerParam = "";
	private String formAction = "";
	private String result = "";
	private String filePath = "";
	private int lineNumber  = 0;
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getHeaderParam() {
		return headerParam;
	}
	public void setHeaderParam(String headerParam) {
		this.headerParam = headerParam;
	}
	public String getFormAction() {
		return formAction;
	}
	public void setFormAction(String formAction) {
		this.formAction = formAction;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}	
	public int getLineNumber() {
		return lineNumber;
	}
	public void setLineNumber(int lineNumber) {
		this.lineNumber = lineNumber;
	}
	
}

  

posted @ 2021-09-19 20:08  奥兰王子  阅读(74)  评论(0编辑  收藏  举报