欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

利用Java怎么對(duì)比兩個(gè)文本文件的相同與不同之處

本篇文章給大家分享的是有關(guān)利用Java怎么對(duì)比兩個(gè)文本文件的相同與不同之處,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供廣州網(wǎng)站建設(shè)、廣州做網(wǎng)站、廣州網(wǎng)站設(shè)計(jì)、廣州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、廣州企業(yè)網(wǎng)站模板建站服務(wù),10余年廣州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

使用需求:

文件1里面是需要比較的內(nèi)容,文件2是被比較的文本,現(xiàn)在需要找到在文件1中每一行的文本在文件2中是否存在并相等,如果相等,就在一份結(jié)果文件中輸出,文件1的哪一行與文件2的哪一行相同,反之不相同就輸出文件1的哪一行不相同貨不存在。

Java代碼如下,輸出的是result.txt文件,這個(gè)文件的行號(hào)和文件1保持一致,所以result中某一行的結(jié)果就是對(duì)應(yīng)的文件1中這行數(shù)據(jù)在文件2中比較之后的結(jié)果。

(需要注意文件1和文件2是通過(guò)每一行的內(nèi)容進(jìn)行比較)

最后為了方便查看可以通過(guò)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=###
-
-
-

以上就是利用Java怎么對(duì)比兩個(gè)文本文件的相同與不同之處,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:利用Java怎么對(duì)比兩個(gè)文本文件的相同與不同之處
鏈接分享:http://www.chinadenli.net/article30/jcoiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司品牌網(wǎng)站制作響應(yīng)式網(wǎng)站面包屑導(dǎo)航網(wǎng)站制作網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管