利用Java怎么对比两个文本文件的相同与不同之处
                                            本篇文章给大家分享的是有关利用Java怎么对比两个文本文件的相同与不同之处,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联专业为企业提供广州网站建设、广州做网站、广州网站设计、广州网站制作等企业网站建设、网页设计与制作、广州企业网站模板建站服务,10余年广州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
使用需求:
文件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怎么对比两个文本文件的相同与不同之处,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
当前标题:利用Java怎么对比两个文本文件的相同与不同之处
网页路径:http://www.scyingshan.cn/article/jcoiso.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 