Java编程实现比对两个文本文件并标记相同与不同之处的方法

时间:2022-04-05 06:46:43

本文实例讲述了Java编程实现比对两个文本文件并标记相同与不同之处的方法。分享给大家供大家参考,具体如下:

使用需求:

文件1里面是需要比较的内容,文件2是被比较的文本,现在需要找到在文件1中每一行的文本在文件2中是否存在并相等,如果相等,就在一份结果文件中输出,文件1的哪一行与文件2的哪一行相同,反之不相同就输出文件1的哪一行不相同货不存在。

Java代码如下,输出的是result.txt文件,这个文件的行号和文件1保持一致,所以result中某一行的结果就是对应的文件1中这行数据在文件2中比较之后的结果。

(需要注意文件1和文件2是通过每一行的内容进行比较)

最后为了方便查看可以通过Notepad++查看:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-
-
-
-
-
-
-
-
-
###=Equal:2=###
###=Equal:12=###
###=Equal:13=###
-
###=Equal:4=###
###=Equal:5=###
###=Equal:1=###
###=Equal:7=###
-
###=Equal:9=###
###=Equal:10=###
###=Equal:11=###
-
-
-

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/aronhu/article/details/48680349