Java编程:比对两个文本文件,标记相同和不同之处

使用需求:

文件1里面是需要比较的内容,文件2是被比较的文本,现在需要找到在文件1中每一行的文本在文件2中是否存在并相等,如果相等,就在一份结果文件中输出,文件1的哪一行与文件2的哪一行相同,反之不相同就输出文件1的哪一行不相同或不存在。

Java代码如下,输出的是result.txt文件,这个文件的行号和文件1保持一致,所以result中某一行的结果就是对应的文件1中这行数据在文件2中比较之后的结果。

(需要注意文件1和文件2是通过每一行的内容进行比较)

最后为了方便查看可以通过Notepad++查看:如图:

 

 

package com.it.aron;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * check repetitive text
 * @author: aronxu
 * @version: 1.0, Sep 22, 2015
 */
public class AutoCheckText {

    private static final String FILE_PATH = "D:/text1.txt";

    private static final String COMPARED_FILE_PATH = "D:/text2.txt";

    private static final String RESULT_FILE_PATH = "D:/result.txt";



    public static void main(String[] args) {
        System.out.println("======Start Search!=======");
        long startTime = System.currentTimeMillis();
        // Read first file
        File file = new File(FILE_PATH);
        File comparedFile = new File(COMPARED_FILE_PATH);
        BufferedReader br = null;
        BufferedReader cbr = null;
        BufferedWriter rbw = null;
        try {
            br = new BufferedReader(new FileReader(file));
            cbr = new BufferedReader(new FileReader(comparedFile));
            cbr.mark(90000000);
            rbw = new BufferedWriter(new FileWriter(RESULT_FILE_PATH));
            String lineText = null;
            while ((lineText = br.readLine()) != null) {
                String searchText = lineText.trim();
                searchAndSignProcess(searchText, cbr, rbw);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("======Process Over!=======");
            System.out.println("Time Spending:" + ((endTime - startTime) / 1000D) + "s");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (cbr != null && rbw != null) {
                        try {
                            cbr.close();
                            rbw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }



    public static void searchAndSignProcess(String searchText, BufferedReader comparedReader, BufferedWriter rbw)
            throws IOException {
        String lineStr = "-\n";
        if (searchText == null) {
            return;
        }
        if ("".equals(searchText)) {
            rbw.write(lineStr);
            return;
        }
        String lineText = null;
        int lineNum = 1;
        while ((lineText = comparedReader.readLine()) != null) {
            String comparedLine = lineText.trim();
            if (searchText.equals(comparedLine)) {
                lineStr = "###=Equal:" + lineNum + "=###\n";
                break;
            }
            lineNum++;
        }
        rbw.write(lineStr);
        comparedReader.reset();
    }

}

 

text1.txt内容:

 

myaccount.msg.register.register=Registro Personas
myaccount.msg.register.your_company=¿Eres empresa?
myaccount.msg.register.sign_up=Registrate aquí
myaccount.msg.register.fields_compellent=Todos los campos son obligatorios
myaccount.msg.register.account_data=Datos de la cuenta
myaccount.msg.register.email=E-mail:


myaccount.msg.register.confirm_email=Confirma tu E-mail:
myaccount.msg.register.password=Contraseña:
myaccount.msg.register.confirm_password=Confirma tu Contraseña:
myaccount.msg.register.personal_data=Datos personales
myaccount.msg.register.first_name=Nombre:


myaccount.msg.register.last_name=Apellido Paterno:
myaccount.msg.register.middle_name=Apellido Materno:
myaccount.msg.register.country=País de Residencia:
myaccount.msg.register.id_card=Cédula de Identidad:


myaccount.msg.register.genero=Género:
myaccount.msg.register.male=Masculino:
myaccount.msg.register.female=Femenino:
myaccount.msg.register.birth=Fecha de Nacimiento:
myaccount.msg.register.day=Día
myaccount.msg.register.month=Mes

text2.txt内容:

 

 

myaccount.msg.register.country=País de Residencia:

myaccount.msg.register.confirm_password=Confirma tu Contraseña:


myaccount.msg.register.last_name=Apellido Paterno:
myaccount.msg.register.middle_name=Apellido Materno:

myaccount.msg.register.id_card=Cédula de Identidad:


myaccount.msg.register.genero=Género:
myaccount.msg.register.male=Masculino:
myaccount.msg.register.female=Femenino:

myaccount.msg.register.personal_data=Datos personales
myaccount.msg.register.first_name=Nombre:

result.txt内容:

-
-
-
-
-
-
-
-
-
###=Equal:2=###
###=Equal:12=###
###=Equal:13=###
-
###=Equal:4=###
###=Equal:5=###
###=Equal:1=###
###=Equal:7=###
-
###=Equal:9=###
###=Equal:10=###
###=Equal:11=###
-
-
-

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值