Java編程實(shí)現(xiàn)比對兩個(gè)文本文件并標(biāo)記相同與不同之處的方法
本文實(shí)例講述了Java編程實(shí)現(xiàn)比對兩個(gè)文本文件并標(biāo)記相同與不同之處的方法。分享給大家供大家參考,具體如下:
使用需求:
文件1里面是需要比較的內(nèi)容,文件2是被比較的文本,現(xiàn)在需要找到在文件1中每一行的文本在文件2中是否存在并相等,如果相等,就在一份結(jié)果文件中輸出,文件1的哪一行與文件2的哪一行相同,反之不相同就輸出文件1的哪一行不相同貨不存在。
Java代碼如下,輸出的是result.txt文件,這個(gè)文件的行號和文件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é)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)
這篇文章主要介紹了spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java中用enum結(jié)合testng實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)的方法示例
TestNG數(shù)據(jù)驅(qū)動(dòng)提供的參數(shù)化讓我們在測試項(xiàng)目可以靈活根據(jù)需求建立不同的dataprovider來提供數(shù)據(jù),而真正實(shí)現(xiàn)數(shù)據(jù),頁面,測試彼此獨(dú)立而又有機(jī)結(jié)合的可能性。 下面這篇文章主要給大家介紹了Java中用enum和testng做數(shù)據(jù)驅(qū)動(dòng)的方法示例,需要的朋友可以參考借鑒。2017-01-01
如何使用Spring RestTemplate訪問restful服務(wù)
這篇文章主要介紹了如何使用Spring RestTemplate訪問restful服務(wù),詳細(xì)的介紹了什么是RestTemplate以及簡單實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
java中對字符串每個(gè)字符統(tǒng)計(jì)的方法
java中對字符串每個(gè)字符統(tǒng)計(jì)的方法,需要的朋友可以參考一下2013-03-03
application.yaml與bootstrap.yaml的使用
這篇文章主要介紹了application.yaml與bootstrap.yaml的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

