The below code was written to take two files, find the values that are not duplicates, and output them to another file.

```
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

//keeps only uniques
//remove all values that are duplicates
public class dupeCleaner {
    public static void main(String[]args){
        String fileA = "Testing1.txt";
        String fileB = "Testing2.txt";
        String[] uniques=clean(fileA,fileB);
        writeFile("file.txt",uniques);
    }
    public static String[] clean(String fileA, String fileB){
        String line;
        ArrayList<String> cleaned = new ArrayList<>();
        String[] list1 = readFile(fileA);
        String[] list2 = readFile(fileB);
        boolean[] firstList = new boolean[list1.length];
        for(int k = 0; k < list1.length;k++){
            firstList[k]=checkDupe(list1[k],list2);
        }
        for(int k = 0; k< list1.length;k++){
            if(firstList[k]){
                cleaned.add(list1[k]);
                System.out.println(list1[k]);
            }
        }
        String[] finalList = new String[cleaned.size()];
        finalList=cleaned.toArray(new String[cleaned.size()]);
        return finalList;
    }
    public static void writeFile(String fileName, String[] list){
        try{
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName,true));
        for(String k: list){
            bw.write(k+"\r\n");
            bw.close();
        }
        }catch(IOException e){
        }
    }
    public static boolean checkDupe(String var, String[] list){
        for(String k : list){
            if(k.equals(var)){
                System.out.println("true");
                return true;
            }
        }
        System.out.println("false");
        return false;
    }
    public static String[] readFile(String fileName){
        ArrayList<String> vars = new ArrayList<>();
        String line;
        try {
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            line = br.readLine();
            while (line != null) {
                vars.add(line);
                line = br.readLine();
            }
        } catch (IOException e) {

        }
        System.out.println(vars.get(5));
        String[] list = vars.toArray(new String[vars.size()]);
        return list;
    }
}

```

The problem lies with the static method readFile, I don't understand why System.out.println(vars.get(5)); returns an IndexOutOfBoundsException, all the code makes sense to me and the file is definitely being read (there's 100 lines in the first file for example and if I just output the String line it all prints in console).

Would anyone be able to shed light on my problem?