I understand that between ADDED and REMOVED you have to have a SAME line between them. What I don't understand is how exactly the content of the files are layered. One attempt I tried timed out and I thought it would work but time is a factor of course.
package com.codegym.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;
/*
Tracking changes
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String filenameA = br.readLine();
String filenameB = br.readLine();
br.close();
br = new BufferedReader(new FileReader(filenameA));
BufferedReader brFileB = new BufferedReader(new FileReader(filenameB));
while (br.ready() && brFileB.ready()) {
String lineA = br.readLine();
String lineB = brFileB.readLine();
if (lineA == null && lineB == null) break;
if (lineA.equals(lineB)) {
lines.add(new LineItem(Type.SAME, lineA));
}
else if (lineA.isEmpty() && !lineB.isEmpty()) {
lines.add(new LineItem(Type.ADDED, lineB));
}
else if (!lineA.isEmpty() && lineB.isEmpty()) {
lines.add(new LineItem(Type.REMOVED, lineA));
}
}
br.close();
brFileB.close();
}
public static enum Type {
ADDED, // New line added
REMOVED, // Line deleted
SAME // No change
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}