На проверяемых файлах проходит.
1.
файл 1 файл 2
a a
b c
c c
2.
файл 1 файл 2
1 1
2 0
3 2
4
package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
FileReader file1 = new FileReader(rd.readLine());
FileReader file2 = new FileReader(rd.readLine());
rd.close();
ArrayList<String> list = new ArrayList();
String text = "";
while (file1.ready()) {
text = text + (char) file1.read();
}
file1.close();
String[] array = text.split("\r\n");
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
ArrayList<String> list2 = new ArrayList();
text = "";
while (file2.ready()) {
text = text + (char) file2.read();
}
file2.close();
String[] array2 = text.split("\r\n");
for (int i = 0; i < array2.length; i++) {
list2.add(array2[i]);
}
boolean flag = false;
boolean flag2 = false;
int n = 0, j = 0;
try {
for (; n < list2.size(); n++, j++) {
if (flag) {
flag = false;
j++;
}
if (flag2) {
flag2 = false;
j--;
}
if (list2.get(n).equals(list.get(j))) {
lines.add(new LineItem(Type.SAME, list2.get(n)));
continue;
} else {
if (list2.get(n).equals(list.get(j + 1))) {
flag = true;
lines.add(new LineItem(Type.REMOVED, list.get(j)));
lines.add(new LineItem(Type.SAME, list2.get(n)));
continue;
} else {
flag2 = true;
lines.add(new LineItem(Type.ADDED, list2.get(n)));
}
}
}
} catch (IndexOutOfBoundsException e) {
for (; j < list.size(); j++) {
lines.add(new LineItem(Type.REMOVED, list.get(j)));
}
for (; n < list2.size(); n++) {
lines.add(new LineItem(Type.ADDED, list2.get(n)));
}
}
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}