首页 > 代码库 > 【工具类】JAVA 以行为单位读取文件并比对

【工具类】JAVA 以行为单位读取文件并比对

package test20140709;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 以行为单位读取文件并比对,显示不同行
 * @author fushihua-wb
 * @date   2014-7-11
 */
public class compare {
	/**
	 * 以行为单位读取文件,常用于读面向行的格式化文件
	 */
	private static String[] readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		String ss[] = new String[0];
		int i = 0;
		try {
			System.out.println("以行为单位读取文件内容,一次读一整行:");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行,直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// if(tempString.contains("<url-pattern>")){
				// //System.out.println("line " + line + ":       " +
				// tempString);
				// //System.out.println("line " + line + ":       " +
				// tempString.trim());
				// //System.out.println(tempString.trim());
				// ss = insert(ss, tempString.trim());
				// i++;
				// }
				// System.out.println("line " + line + ":       " + tempString);

				ss = insert(ss, tempString.trim());
				i++;

				line++;

			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			System.out.println("共" + i + "行");
			System.out.println("START");
			// for (String string : ss) {
			// System.out.println(string);
			// }

			// for (int j = 0;j <= ss.length;j++) {
			// System.out.println(ss[j]);
			// }
			System.out.println("END");
		}
		return ss;
	}
	/*
	 * 插入到数组
	 */
	private static String[] insert(String[] arr, String str) {
		int size = arr.length;

		String[] tmp = new String[size + 1];

		System.arraycopy(arr, 0, tmp, 0, size);

		tmp[size] = str;

		return tmp;
	}

	// 删除数组中其中一个元素
	public static String[] delString(String[] str, String delString) {
		// String [] str = {"Java", "C++", "Php", "C#", "Python"};
		// for (String elementA:str ) {
		// System.out.print(elementA + " ");
		// }
		// 删除php
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < str.length; i++) {
			list.add(str[i]);
		}
		// list.remove(2);
		list.remove(delString);
		// System.out.println();
		String[] newStr = list.toArray(new String[1]); // 返回一个包含所有对象的指定类型的数组
		// for (String elementB:newStr ) {
		// System.out.print(elementB + " ");
		// }
		// System.out.println();
		return newStr;
	}
	
	/*
	 * 输出两个数组相同元素
	 */
	private static void compare(String fileName1, String fileName2) {
		String[] ss1 = readFileByLines(fileName1);
		String[] ss2 = readFileByLines(fileName2);
		for (String s1 : ss1) {
			for (String s2 : ss2) {
				if (s1.endsWith(s2)) {
					System.out.println(s1);
				}
			}
		}
		for (String s1 : ss2) {
			System.out.println(s1);
		}
	}
	/*
	 * 输出两个数组不同元素
	 */
	public static void compare1(String fileName1, String fileName2) {
		// String a[] = new String[]{"a","a2","b3"};
		// String b[] = new String[]{"b","b3","a2"};
		System.out.println("START");
		String[] a = readFileByLines(fileName1);
		String[] b = readFileByLines(fileName2);
		ArrayList c = new ArrayList();
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < b.length; j++) {
				if (a[i].equals(b[j])) {
					c.add(a[i]);
				}
			}
		}
		System.out.println("只在" + fileName1 + "中:");
		for (int i = 0; i < a.length; i++) {
			if (!c.contains(a[i])) {
				System.out.println(a[i] + "");
			}
		}
		System.out
				.println("----------------------------------------------------");
		System.out.println("只在" + fileName2 + "中:");
		for (int i = 0; i < a.length; i++) {
			if (!c.contains(b[i])) {
				System.out.println(b[i] + "");
			}
		}
		System.out.println("END");
	}

	public static void main(String[] args) {
		// readFileByLines("D:\\web-release.xml");
		// readFileByLines("D:\\web-prod.xml");
		// readFileByLines("D:\\web-prod1.xml");
		// compare1("D:\\web-prod1.xml","D:\\web-release1.xml");
		compare1("D:\\web-release1.xml", "D:\\web-prod1.xml");
	}
}