Java編程實現(xiàn)比對兩個文本文件并標(biāo)記相同與不同之處的方法
本文實例講述了Java編程實現(xiàn)比對兩個文本文件并標(biāo)記相同與不同之處的方法。分享給大家供大家參考,具體如下:
使用需求:
文件1里面是需要比較的內(nèi)容,文件2是被比較的文本,現(xiàn)在需要找到在文件1中每一行的文本在文件2中是否存在并相等,如果相等,就在一份結(jié)果文件中輸出,文件1的哪一行與文件2的哪一行相同,反之不相同就輸出文件1的哪一行不相同貨不存在。
Java代碼如下,輸出的是result.txt文件,這個文件的行號和文件1保持一致,所以result中某一行的結(jié)果就是對應(yīng)的文件1中這行數(shù)據(jù)在文件2中比較之后的結(jié)果。
(需要注意文件1和文件2是通過每一行的內(nèi)容進(jìn)行比較)
最后為了方便查看可以通過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內(nèi)容:
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內(nèi)容:
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內(nèi)容:
- - - - - - - - - ###=Equal:2=### ###=Equal:12=### ###=Equal:13=### - ###=Equal:4=### ###=Equal:5=### ###=Equal:1=### ###=Equal:7=### - ###=Equal:9=### ###=Equal:10=### ###=Equal:11=### - - -
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
SpringMVC將請求和響應(yīng)的數(shù)據(jù)轉(zhuǎn)換為JSON格式的幾種方式
這篇文章主要給大家介紹餓了SpringMVC將請求和響應(yīng)的數(shù)據(jù)轉(zhuǎn)換為JSON格式的幾種方式,文中通過代碼示例和圖文結(jié)合給大家介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-11-11
Springboot如何利用攔截器攔截請求信息收集到日志詳解
一些系統(tǒng)經(jīng)常需要關(guān)注用戶請求的具體信息,如用戶信息、請求參數(shù)、響應(yīng)結(jié)果等等,在SpringBoot應(yīng)用中可通過攔截器的方式統(tǒng)一處理,下面這篇文章主要給大家介紹了關(guān)于Springboot如何利用攔截器攔截請求信息收集到日志的相關(guān)資料,需要的朋友可以參考下2021-08-08
MyBatis的SQL執(zhí)行結(jié)果和客戶端執(zhí)行結(jié)果不一致問題排查
本文主要介紹了MyBatis的SQL執(zhí)行結(jié)果和客戶端執(zhí)行結(jié)果不一致問題排查,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Ubuntu安裝jenkins完成自動化構(gòu)建詳細(xì)步驟
Jenkins是一個開源的自動化服務(wù)器,可以用來輕松地建立持續(xù)集成和持續(xù)交付(CI/CD)管道,這篇文章主要給大家介紹了關(guān)于Ubuntu安裝jenkins完成自動化構(gòu)建的相關(guān)資料,需要的朋友可以參考下2024-03-03
Springboot視圖解析器ViewResolver使用實例
這篇文章主要介紹了Springboot視圖解析器ViewResolver使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析
本文將探討如何在Java中有效地應(yīng)對這些挑戰(zhàn),介紹一種強大的工具并發(fā)Map,它能夠幫助您管理多線程環(huán)境下的共享數(shù)據(jù),確保數(shù)據(jù)的一致性和高性能,深入了解Java中的并發(fā)Map實現(xiàn),包括ConcurrentHashMap和ConcurrentSkipListMap,及相關(guān)知識點2023-09-09

