Не могу подобрать пример, что бы ответ получился неверный.
package com.javarush.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static List<String> makeListFromFile (String file) throws IOException {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> stringsFile = new ArrayList<>();
while (bufferedReader.ready()){
stringsFile.add(bufferedReader.readLine());
}
bufferedReader.close();
fileReader.close();
return stringsFile;
}
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();
List<String> stringsFile1 = makeListFromFile(file1);
List<String> stringsFile2 = makeListFromFile(file2);
int i = 0;
int j = 0;
int k = Math.max(stringsFile1.size(), stringsFile2.size());
for (int t = 0; t <= k; t++) {
if (i < stringsFile1.size() && j < stringsFile2.size()) {
if (stringsFile1.get(i).equals(stringsFile2.get(j))) {
lines.add(new LineItem(Type.SAME, stringsFile1.get(i)));
i++;
j++;
} else if (stringsFile1.get(i + 1).equals(stringsFile2.get(j))) {
lines.add(new LineItem(Type.REMOVED, stringsFile1.get(i)));
i++;
} else if (stringsFile1.get(i).equals(stringsFile2.get(j + 1))) {
lines.add(new LineItem(Type.ADDED, stringsFile2.get(j)));
j++;
}
}
}
if (i != stringsFile1.size() && j == stringsFile2.size()){
lines.add(new LineItem(Type.REMOVED, stringsFile1.get(stringsFile1.size() - 1)));
}
if (i == stringsFile1.size() && j != stringsFile2.size()){
lines.add(new LineItem(Type.ADDED, stringsFile2.get(stringsFile2.size() - 1)));
}
for (LineItem item : lines){
System.out.println(item);
}
}
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;
}
@Override
public String toString() {
return type + " " + line;
}
}
}