Reputation: 93
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?
Upvotes: 1
Views: 72
Reputation: 794
Your first problem is that your code is not keeping the unique values, it's keeping the duplicates. Your text files probably doesn't have enough duplicates and that's why you're getting an IndexOutOfBoundsException
. Change the condition in the clean()
method to:
if (!firstList[k])
The second problem you'll encounter is that you're trying to write to a closed file because you close it in a foreach loop. Remove the bw.close()
statement from the loop in writeFile()
:
for (String k : list) {
bw.write(k + "\r\n");
}
Upvotes: 0
Reputation: 5082
You need to include the bw.close
statement out of the for
loop. What you are doing is writing a line to the file and closing the Writer
which will write just the first string to the file.
try(BufferedWriter bw = new BufferedWriter(new FileWriter(fileName,true));){
for(String k: list){
bw.write(k+"\r\n");
}
}catch(IOException e){
//Handle exception
}
Upvotes: 1