Working with two txt files

1

I have two different .txt files as examples: file1.txt has 1854 lines with 6 numbers on each line separated by "" (one space). In the other file2.txt I have more than 1 million lines with 6 numbers on each line separated by "" (a space too). I tried to have the first line of file2.txt look at all the lines in file1.txt by looking for how many equal numbers there are in each line of file1.txt and then move on to the next line of file2.txt and again do the same analysis as was done before, until the end of file2.txt. But the problem is that my code is just parsing the first line of file1.txt with the first line of file2.txt, then jumps to the next line of both files. Can anyone help me crafting this code the way I would like it. The code is as follows:

public class Confere {

public static void main(String[] args) throws FileNotFoundException {

try {
// pega os arquivos txt´s
File file = new File("C:/Users/Usuario/Documents/Vander/mega.txt");
File file2 = new File("C:/Users/Usuario/Documents/Vander/resultadomega.txt");

FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);

FileReader fileReader2 = new FileReader(file2);
BufferedReader bufferedReader2 = new BufferedReader(fileReader2);

while (bufferedReader.ready()) {

bufferedReader2.ready();

String linha = bufferedReader.readLine(); // lê uma linha...
String linha2 = bufferedReader2.readLine(); // lê uma linha...

if (linha.toString().contains(linha2.toString())) { // verifica se as linhas são iguais
System.out.println("igual");
// #####################################################
RandomAccessFile raf = new RandomAccessFile("C:/Users/Usuario/Documents/Vander/relatorio.txt", "rw");

raf.seek(raf.length());
raf.writeBytes(linha + "\r\n");
raf.close();
// ######################################################
System.out.println(linha);
} else {
System.out.println("diferente");
}
}

} catch (IOException e) {
throw new RuntimeException(e);
}

}

I would be very grateful, as it is very difficult ...

    
asked by anonymous 10.09.2016 / 01:45

2 answers

1

To get all the rows in a file, use Files#readAllLines ":

List<String> linhas = Files.readAllLines(Paths.get("C:/foo.txt"));

To get an array containing string items separated by white space, use \s+ as pattern for the String#split :

String []valores = "Stack Overflow".split("\s+"); // ["Stack", "Overflow"]

To get duplicate items in two arrays , one solution is to create a temporary list and pass the array as argument (in list) for the ArrayList constructor. Then using the method retainAll you get the elements that exist in the two collections, for example:

String []a = {"stack", "overflow", "em", "português"};
String []b = {"stack", "overflow"};

List<String> duplicados = new ArrayList<>(Arrays.asList(a));
duplicados.retainAll(Arrays.asList(b)); // ["stack", "overflow"]

With this you can get all the lines of the files, "break" them with white space and check if the items in one list exist in another.

Example

List<String> linhasA = Files.readAllLines(Paths.get("C:/a.txt"));
List<String> linhasB = Files.readAllLines(Paths.get("C:/b.txt"));

linhasB.forEach(linhaB -> {
   linhasA.forEach(linhaA -> {
       String []valoresLinhaB = linhaB.split("\s+");
       String []valoresLinhaA = linhaA.split("\s+");

       List<String> duplicados = new ArrayList<>(Arrays.asList(valoresLinhaB));
       duplicados.retainAll(Arrays.asList(valoresLinhaA));

       if(duplicados.size() > 0){
          String mensagem = String.format("Linha B: %10s | Linha A: %10s | Duplicados: %15s",
                            linhaB, linhaA, duplicados);
          System.out.println(mensagem);
       }
   });
});

Example of output :

Linha B:        2 4 | Linha A:  1 2 3 4 5 | Duplicados:     [2, 4]
Linha B:        2 4 | Linha A:    2 3 4 5 | Duplicados:     [2, 4]
Linha B:        2 4 | Linha A:      1 4 5 | Duplicados:        [4]
Linha B:        2 4 | Linha A:        1 4 | Duplicados:        [4]
Linha B:        2 4 | Linha A:      3 4 5 | Duplicados:        [4]
===
Linha B:      2 4 5 | Linha A:  1 2 3 4 5 | Duplicados:  [2, 4, 5]
Linha B:      2 4 5 | Linha A:    2 3 4 5 | Duplicados:  [2, 4, 5]
Linha B:      2 4 5 | Linha A:      1 4 5 | Duplicados:     [4, 5]
Linha B:      2 4 5 | Linha A:        1 4 | Duplicados:        [4]
Linha B:      2 4 5 | Linha A:      3 4 5 | Duplicados:     [4, 5]
===
Linha B:    1 2 5 4 | Linha A:  1 2 3 4 5 | Duplicados: [1, 2, 5, 4]
Linha B:    1 2 5 4 | Linha A:    2 3 4 5 | Duplicados:    [2, 5, 4]
Linha B:    1 2 5 4 | Linha A:      1 4 5 | Duplicados:    [1, 5, 4]
Linha B:    1 2 5 4 | Linha A:        1 4 | Duplicados:       [1, 4]
Linha B:    1 2 5 4 | Linha A:      3 4 5 | Duplicados:       [5, 4]
...
    
15.09.2016 / 16:23
1

I had no way to test, but try storing the contents of the files in two lists, for example:

public static void main(String[] args) throws Exception{

    File file = new File("C:/Users/Usuario/Documents/Vander/mega.txt");
    File file2 = new File("C:/Users/Usuario/Documents/Vander/resultadomega.txt");

    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    FileReader fileReader2 = new FileReader(file2);
    BufferedReader bufferedReader2 = new BufferedReader(fileReader2);

    List<String> arquivo1 = new ArrayList<>();
    List<String> arquivo2 = new ArrayList<>();

    while (bufferedReader.ready())
        arquivo1.add(bufferedReader.readLine());

    while (bufferedReader2.ready())
        arquivo2.add(bufferedReader2.readLine());

    arquivo1.stream().forEach(linhaArquivo1->{
        long qtdOcorrencia=arquivo2.stream().filter(linhaArquivo2-> linhaArquivo2.equals(linhaArquivo1)).count();
        System.out.println("Conteúdo: "+linhaArquivo1+" Quantidade Ocorrência: "+qtdOcorrencia);
    });
}
    
10.09.2016 / 03:44