Java编程实现比对两个文本文件并标记相同与不同之处的方法

网友投稿 1039 2023-03-27


Java编程实现比对两个文本文件并标记相同与不同之处的方法

本文实例讲述了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:/resRHZZmult.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:

myaRHZZmccount.msg.register.RHZZmlast_name=Apellido Paterno:

myaccount.msg.register.middle_name=Apellido Materno:

myaccount.msg.register.country=Pas de Residencia:

myaccount.msg.register.id_card=Cdula de Identidad:

myaccount.msg.register.genero=Gnero:

myaccount.msg.register.male=Masculino:

myaccount.msg.register.female=Femenino:

myaccount.msg.register.birth=Fecha de Nacimiento:

myaccount.msg.register.day=Da

myaccount.msg.register.month=Mes

text2.txt内容:

myaccount.msg.register.country=Pas 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=Cdula de Identidad:

myaccount.msg.register.genero=Gnero:

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=###

-

-

-

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringCloud实战小贴士之Zuul的路径匹配
下一篇:移动端效果之Swiper详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~