Comparing two text files randomly with Java

I am trying to compare two text files that are randomized and print lines that match in both files. File 1:

Student1
Student2
Student3
Student4

File 2:

Student6
Student1
Student2

I want the result to be

Student1
Student2

My code is below.

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

     String first = "file1.txt";
     String second = "file2.txt";
     BufferedReader fBr = new BufferedReader(new FileReader(first));
     BufferedReader sBr = new BufferedReader(new FileReader(second));   




     PrintWriter writer = new PrintWriter("test.txt", "UTF-8");  
     while ((first = fBr.readLine()) != null) {
         String partOne1 = fBr.readLine();
         String partTwo1 = sBr.readLine();
         while ((second = sBr.readLine()) != null) {
                System.out.println(first);
                writer.println(first);  
                break;                   

         }
     }


     writer.close();
     fBr.close();
     sBr.close(); 
+4
source share
4 answers

It's quite simple =) Try to save all the results from the first file and compare with all the lines from the second. It will be like this:

package com.company;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

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

        String first = "file1.txt";
        String second = "file2.txt";
        BufferedReader fBr = new BufferedReader(new FileReader(first));
        BufferedReader sBr = new BufferedReader(new FileReader(second));

        ArrayList<String> strings = new ArrayList<String>();

        while ((first = fBr.readLine()) != null) {
            strings.add(first);
        }
        fBr.close();

        while ((second = sBr.readLine()) != null) {
            if (strings.contains(second)) {
                System.out.println(second);
            }
        }
        sBr.close();
    }
}

It is better to use memory when possible, your “time” inside, while it may work too long and obfuscate logic.

+5
source

- arraylist preserveAll() . , - .

public static void main(String[] args) throws IOException {
     String first = "file1.txt";
     String second = "file2.txt";
     BufferedReader fBr = new BufferedReader(new FileReader(first));
     BufferedReader sBr = new BufferedReader(new FileReader(second));   

     List<String> firstFile = new ArrayList<>();
     List<String> secondFile = new ArrayList<>();

     PrintWriter writer = new PrintWriter("test.txt", "UTF-8");  
     while ((first = fBr.readLine()) != null) {
         firstFile.add(first);
     }
     while ((second = sBr.readLine()) != null) {
         secondFile.add(second);                  
     }

     List<String> commonFile = new ArrayList<>(firstFile);
     commonFile.retainAll(secondFile);
     System.out.println(commonFile);

     writer.close();
     fBr.close();
     sBr.close(); 
}
+2

Java8, . , Java8. - , . ,

List<String> file1Lines = Files.readAllLines(Paths.get("C:\\DevelopmentTools\\student-file1.txt"), Charset.defaultCharset());
List<String> file2Lines = Files.readAllLines(Paths.get("C:\\DevelopmentTools\\student-file2.txt"), Charset.defaultCharset());

List<String> matchingStrings = file1Lines.stream().
filter(studentInfo -> file2Lines.contains(studentInfo))
                    .collect(Collectors.toList());
matchingStrings.forEach(System.out::println);

:

Student1 , Student2
+1

:

, . -, , , - , .

O (n * m). O (n log n + m log m) . hashmap , O (n + m), .

0
source

Source: https://habr.com/ru/post/1652874/


All Articles