![](https://cdn.javarush.com/images/system/e3c25b10-5a95-4d39-82f3-a75d7b8639f8/1024.jpeg)
![](https://cdn.javarush.com/images/system/a06f3c0c-d0d8-4e28-b2b1-381b4a7fc88b/1024.jpeg)
![](https://cdn.javarush.com/images/system/ea90d146-62da-48d8-b814-5409c9a963f8/original.jpeg)
![](https://cdn.javarush.com/images/system/ceeddbd1-56f4-42d3-b897-fb502d2862ab/1024.jpeg)
![](https://cdn.javarush.com/images/system/a82869f6-f131-46f6-8edf-8835cfc0d412/original.jpeg)
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 bfr = new BufferedReader(new InputStreamReader(System.in));
String file1 = bfr.readLine();
String file2 = bfr.readLine();
bfr.close();
FileReader fileReader1 = new FileReader(file1);
FileReader fileReader2 = new FileReader(file2);
StringBuilder strBuild = new StringBuilder();
ArrayList<String> list1 = new ArrayList<>();
while (fileReader1.ready()) {
String s = String.valueOf((char) fileReader1.read());
if(!s.equals("\r") && !s.equals("\n")) strBuild.append(s);
if(s.equals("\r") || !fileReader1.ready()) {
list1.add(strBuild.toString());
strBuild = new StringBuilder();
}
}
fileReader1.close();
strBuild = new StringBuilder();
ArrayList<String> list2 = new ArrayList<>();
while (fileReader2.ready()) {
String s = String.valueOf((char) fileReader2.read());
if(!s.equals("\r") && !s.equals("\n")) strBuild.append(s);
if(s.equals("\r") || !fileReader2.ready()) {
list2.add(strBuild.toString());
strBuild = new StringBuilder();
}
}
fileReader2.close();
for (int i = 0; i < list1.size(); i++) {
if(list1.get(i).equals(list2.get(i))) {
lines.add(new LineItem(Type.SAME, list1.get(i)));
}
else {
String previosString = "";
if(i != 0) {
previosString = list1.get(i-1);
}
String string1 = list1.get(i);
String string2 = list2.get(i);
int previosIndex = -1;
if(!previosString.equals("")) {
previosIndex = Character.getNumericValue(previosString.charAt(previosString.length()-1));
}
int index1 = Character.getNumericValue(string1.charAt(string1.length()-1));
int index2 = Character.getNumericValue(string2.charAt(string2.length()-1));
if(previosIndex != -1) {
if(index1 == previosIndex + 1 || (previosIndex == 5 && index1 == 0)) {
lines.add(new LineItem(Type.REMOVED, string1));
}
if(index2 == previosIndex + 1 || index2 == 0) {
lines.add(new LineItem(Type.ADDED, string2));
}
}
}
}
// for (LineItem s : lines) {
// System.out.println(s.type + " " + s.line);
// }
}
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;
}
}
}