что то я намудрил, но хотелось бы уже допилить задачу.
package com.javarush.task.task19.task1916;
import com.sun.imageio.plugins.common.InputStreamAdapter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
reader.close();
BufferedReader stream1 = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
BufferedReader stream2 = new BufferedReader(new InputStreamReader(new FileInputStream(file2)));
String s;
String f;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((s = stream1.readLine()) != null) {
list1.add(s);
}
while ((f = stream2.readLine()) != null) {
list2.add(f);
}
stream1.close();
stream2.close();
int g = 0;
int i = 0;
int d = list2.size() - list1.size();
try {
for (i = 0; i < list1.size(); i++) {
if (list1.get(i).equals(list2.get(g))) {
LineItem line = new LineItem(Type.SAME, list1.get(i));
lines.add(line);
g++;
} else if (!list1.get(i).equals(list2.get(g)) && list2.get(g).equals(list1.get(i + 1))) {
LineItem line = new LineItem(Type.REMOVED, list1.get(i));
lines.add(line);
} else if (!list1.get(i).equals(list2.get(g)) && list1.get(i).equals(list2.get(g + 1))) {
LineItem line = new LineItem(Type.ADDED, list2.get(g));
lines.add(line);
i--;
g++;
}
}
if (d > 0) {
for (int p = g; g < list2.size(); g++) {
LineItem line = new LineItem(Type.ADDED, list2.get(g));
lines.add(line);
}
}
} catch (Exception e) {
if (i < list1.size()) {
for (int k = i; k < list1.size(); k++) {
lines.add(new LineItem(Type.REMOVED, list1.get(k)));
}
} else if (g < list2.size()) {
for (int k = g; k < list1.size(); k++) {
lines.add(new LineItem(Type.ADDED, list2.get(k)));
}
}
}
}
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;
}
}
}