1
I have a file called arq1.txt
and the other arq2.txt
.
Values within the arq1
:
2
5
1
10
21
7
8
8
3
Values of arq2
:
1,2,3
In this case, my job is to compare the values present in the 2 files and create a new file making the text written in it the value of arq1 - arq2
.
In this case, it would remove the values 1, 2 and 3 of the arq1
, being as follows (in case the code would create a 3° file and store this value in it):
arq3.txt
:
5
10
21
7
8
8
So far I’ve achieved this:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class App {
public static void main(final String[] args) throws IOException {
final List<String> linhasA = Files.readAllLines(Paths.get("C:\\q\\arq1.txt"));
final List<String> linhasB = Files.readAllLines(Paths.get("C:\\q\\arq2.txt"));
linhasB.forEach(linhaB -> {
linhasA.forEach(linhaA -> {
final String []valoresLinhaB = linhaB.split("\\,");
final String []valoresLinhaA = linhaA.split("\\,");
final List<String> duplicados = new ArrayList<>(Arrays.asList(valoresLinhaB));
duplicados.retainAll(Arrays.asList(valoresLinhaA));
if(duplicados.size() > 0){
System.out.println(duplicados);
}
});
});
}
}
Thus the value returned is:
[2]
[1]
[3]
That in the case the code basically made the comparison between the 2 files and verified the duplicate data.
Now I have a fucking huge difficulty in making it be printed on the screen the value of arq1 removing duplicate values.
I tried to make some comparisons but they didn’t work (I’m new in java), something like:
for(int i=0; i<10; i++){
if(valoresLinhaA[i] != duplicados){
System.out.println(valoreslinhaA[i]);
}
}
But the big mistake is that the valoresLinhaA[i]
is a string and has no way to compare that way (at least the IDE tells me so).
Some other way to compare the values and print only the value without duplication?
About creating the archive, I believe I can turn around after getting the result without duplicate values.