java 比较文件内容程序

package com;

import java.io.BufferedReader;

public class CompareFile {

	public static void main(String[] args) {
		try {
			Scanner readr = new Scanner(System.in);
			System.out.println("输入第一个文件的路径及文件名:");
			String file1 = readr.next();
			System.out.println("输入第二个文件的路径及文件名:");
			String file2 = readr.next();
			FileReader inOne1 = new FileReader(file1);
			BufferedReader inTwo1 = new BufferedReader(inOne1);
			String s1 = null;
			int i = 0;// 行数
			boolean b = true;// 检察是行数
			FileReader inOne2 = new FileReader(file2);
			BufferedReader inTwo2 = new BufferedReader(inOne2);
			String s2 = null;
			// 文件内容对比
			while ((s1 = inTwo1.readLine()) != null) {
				i++;
				while ((s2 = inTwo2.readLine()) != null) {

					if (s1.equals(s2) != true) {
						System.out.println("第" + i + "行:" + s1 + "和" + s2
								+ "内容不一样");
						b = false;
					}
					break;

				}
			}
			if (b) {
				System.out.println("文件内容一样");
			}
		} catch (IOException e) {
			System.out.println(e);
		}
	}

}

你可能感兴趣的:(java)