Results 1 to 3 of 3
  1. #1
    Detox is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Comparing Missing Items of Two Text Files in a List

    Hello all, new here to the forum and a fairly new developer in the profession.

    What i'm trying to do with my code is read the files of two .txt documents, create a list (or two) and compare the two, and find the missing users, possibly through a loop?

    example.txt
    user1
    user2
    user5
    user7
    user8
    user10


    example1.txt
    user2
    user3
    user4
    user5
    user6
    user7
    user8
    user9
    user10

    Here is what i've got working so far:
    Java Code:
    import java.io.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class ReadTwoFiles implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    static String FILE_ONE = "C:/example.txt";
    static String FILE_TWO = "C:/example1.txt";
    
    public static void main(String[] args) {
    	List<String> lines = FILE_ONE.readLines(new File(""));
    	for (String line : lines) {
    	  System.out.println(line);  
    	}
        try {
            CompareFile(FILE_ONE, FILE_TWO);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    private static void CompareFile(String fILE_ONE2, String fILE_TWO2)
            throws Exception {
    
        File f1 = new File(FILE_ONE);
        File f2 = new File(FILE_TWO);
    
        FileReader fR1 = new FileReader(f1);
        FileReader fR2 = new FileReader(f2);
    
        BufferedReader reader1 = new BufferedReader(fR1);
        BufferedReader reader2 = new BufferedReader(fR2);
    
        String line1 = null;
        String line2 = null;
    
    
        while (((line1 = reader1.readLine()) != null)
                &&((line2 = reader2.readLine()) != null)) {
            if (!line1.equalsIgnoreCase(line2)) {
                System.out.println("The file on this line is  DIFFERENT");
            } else {
                System.out.println("The file on this line is identical");
            }
    
        }
        reader1.close();
        reader2.close();
    
       }
    }
    My question is am I going about this the right way? How can I create a list that compares two text files telling me what's missing between the two?

    Also, can this be done with other formats like an excel .xlsx spreadsheet?

    Any assistance would be greatly appreaciated.

    Thanks,
    -D

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Comparing Missing Items of Two Text Files in a List

    Finding where files are different is not the same as finding what is different about them. You wanted to determine which users were omitted from each file. Assuming you are using each file as a base to test the other file, the only missing users are user1 from the second file and user6 from the first file. But the files differ in every line (see example below).

    user1 user2
    user2 user3
    user3 user4
    user4 user5
    user5 user6

    You should read up on the Set interfaces and its implementations in the JDK API. By assigning one list to a set and then iterating thru the set using the other list you can determine what is in the set. You can then delete members of the set accordingly and ultimately obtain the missing users.

    Regards,
    Jim
    The Java Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Comparing Missing Items of Two Text Files in a List

    Quote Originally Posted by jim829 View Post
    By assigning one list to a set and then iterating thru the set using the other list you can determine what is in the set. You can then delete members of the set accordingly and ultimately obtain the missing users.
    ... or use the removeAll(...) / retainAll(...) methods defined in Set.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help on ideation of making an appending list of items
    By dddJewelsbbb in forum New To Java
    Replies: 5
    Last Post: 06-17-2012, 12:10 AM
  2. SORT list on items where frequencies are involved
    By emgee in forum Java Applets
    Replies: 1
    Last Post: 11-14-2010, 02:42 PM
  3. Transfering all items from one List to another
    By Bulelakes in forum New To Java
    Replies: 6
    Last Post: 08-23-2010, 08:48 AM
  4. List of Items in JTextArea
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 7
    Last Post: 03-15-2010, 09:03 PM
  5. Comparing Two Text Files
    By coder09 in forum New To Java
    Replies: 15
    Last Post: 03-03-2009, 06:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •