本篇文章给大家分享的是有关利用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=###
-
-
-
以上就是利用Java怎么对比两个文本文件的相同与不同之处,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。