Results 1 to 7 of 7
  1. #1
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default Comparing 2 CSV files

    Hi,

    I am trying to compare two CSV files and write the difference to a 3rd file but I am having issues and I am not sure why can someone please help me.

    Java Code:
    public class CompareLog {
      public static String[] readCSVFile(String fileName) throws FileNotFoundException, IOException {
    	BufferedReader me = null;
          FileReader readerFile = new FileReader(fileName);
    	me = new BufferedReader(readerFile);
    	String line;
    	String out[] = null;
    	while((line = me.readLine()) != null)
    	  {
    	     out = line.toString().split(",");
              }
    	     return out;
      }
      public static void writeToFile(Set<String[]> records, String fileName) throws IOException {
        // your code here
    	     File outputFile=new File("C:/temp/compare_3.log");
    	     FileWriter out = new FileWriter(outputFile);
    	     out.write(records+"\n");
    	     out.close();
      }
    
      public static void main(String[] args) throws FileNotFoundException, IOException {
        // Read both files
    	String[] recordsA = readCSVFile("C:/temp/compare_1.log");
            String[] recordsB = readCSVFile("C:/temp/compare_2.log");
    
            // Create a set to store all unique records in
            Set<String[]> uniqueRecords = new HashSet<String[]>();
            uniqueRecords.addAll(recordsA);
            uniqueRecords.addAll(recordsB);
    
            // write the 'uniqueRecorsd' to file
            writeToFile(uniqueRecords, "outputFile");
      }
    }
    I am having the following error by
    uniqueRecords.addAll(recordsA);
    uniqueRecords.addAll(recordsB);

    Java Code:
    cannot find symbol
      symbol:   method addAll(java.lang.String[])
      location: interface java.util.Set<java.lang.String[]>
    Any help would be greatly appreciated.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    addAll wants a Collection --> Set (Java Platform SE 6) not an String array :)

    Set<String[]> ?? --> then you have to use only add(...)
    But if you really want Set<String> you can write addAll(Arrays.asList(recordsA))

  3. #3
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    Hi eRaaaa, thank you for your advise, unfortunately that still did not work, so made loads of changes and it now shifted to the return statment.

    Java Code:
    public class CompareLogl1 {
      public static Set<String[]> readCSVFile(String fileName) throws FileNotFoundException, IOException {
    	BufferedReader me = null;
            FileReader readerFile = new FileReader(fileName);
    	me = new BufferedReader(readerFile);
    	String line;
    	String out[] = null;
    	while((line = me.readLine()) != null)
    	  {
    	     out = line.toString().split(line);
              }
    	     [COLOR="Red"]return out;[/COLOR]
      }
      public static void writeToFile(Set<String[]> records, String fileName) throws IOException {
        // your code here
    	     File outputFile=new File("C:/temp/compare_3.log");
    	     FileWriter out = new FileWriter(outputFile);
    	     out.write(records+"\n");
    	     out.close();
      }
    
      public static void main(String[] args) throws FileNotFoundException, IOException {
        // Read both files
    	Set<String[]> recordsA = readCSVFile("C:/temp/compare_1.log");
            Set<String[]> recordsB = readCSVFile("C:/temp/compare_2.log");
    
            // Create a set to store all unique records in
            Set<String[]> uniqueRecords = new HashSet<String[]>();
            uniqueRecords.addAll(recordsA);
            uniqueRecords.addAll(recordsB);
    
            // write the 'uniqueRecorsd' to file
            writeToFile(uniqueRecords, "outputFile");
      }
    }
    Same error and I already tried assigning Set<String[]> to line & out but then I shift the error to out = line.toString().split(line);

    So whatever I do the error seems to shift from one place to another.

    Any further help would be great.

    Many Thanks

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
      public static [B]Set<String[]>[/B] readCSVFile(String fileName) throws FileNotFoundException, IOException {
    	BufferedReader me = null;
            FileReader readerFile = new FileReader(fileName);
    	me = new BufferedReader(readerFile);
    	String line;
    	[B]String out[] = null;[/B]
    	while((line = me.readLine()) != null)
    	  {
    	     out = line.toString().split(line);
              }
             return out;
      }
    You're returning a String[], but your method says it should be returning a Set<String>.
    You can't do that.

    If you need a Set<String> back then you have to create one in the method and populate it.

    Also, you are setting you String[] to the split(), each time round the loop. So you're only ever returning the last split() line. Which I'm sure isn't correct?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Spam reported

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tolls View Post
    Spam reported
    squashed .

  7. #7
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    Hello I have been trying to change the whole class and although I almost got it the way I want something is not quite right.

    What I tying to do:

    1 - read through file1, split "," and get field 3.
    2 - read through file2 line by line and see if (file1.field3 contains in any line in file2)

    then if true, print out the line that contains in file2 but not in file1

    This is my bit of code.

    Java Code:
            for (String line = recordsA.readLine();line != null;line = recordsA.readLine()){
            String[] fields = line.split(",");
            String fieldsA = (fields[2]);
    
                for (String line2 = recordsB.readLine();line2 != null;line2 = recordsB.readLine()){
                    String fieldsB = (line2);
    
    
                    if ((fieldsB).contains(fieldsA)){
                    System.out.println((fieldsB));}
                }
            }

    For some reason it goes through and only print out the first line in File1 that does existe in both logs.

    Please can someone tell me ehat I am doing wrong.

    Many Thanks

Similar Threads

  1. comparing the two files
    By trkece in forum New To Java
    Replies: 0
    Last Post: 02-21-2011, 10:27 AM
  2. Comparing two XML files
    By anuj_sharma in forum XML
    Replies: 1
    Last Post: 02-07-2011, 09:09 AM
  3. Comparing Two Text Files
    By coder09 in forum New To Java
    Replies: 15
    Last Post: 03-03-2009, 06:11 AM
  4. [SOLVED] Comparing two files
    By Doctor Cactus in forum New To Java
    Replies: 7
    Last Post: 02-27-2009, 10:42 PM

Posting Permissions

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