Results 1 to 7 of 7
Thread: Comparing 2 CSV files
- 04-08-2011, 04:35 PM #1
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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.
I am having the following error byJava 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"); } }
uniqueRecords.addAll(recordsA);
uniqueRecords.addAll(recordsB);
Any help would be greatly appreciated.Java Code:cannot find symbol symbol: method addAll(java.lang.String[]) location: interface java.util.Set<java.lang.String[]>
- 04-08-2011, 04:44 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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))
- 04-11-2011, 11:03 AM #3
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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.
Same error and I already tried assigning Set<String[]> to line & out but then I shift the error to out = line.toString().split(line);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"); } }
So whatever I do the error seems to shift from one place to another.
Any further help would be great.
Many Thanks
- 04-11-2011, 12:04 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
You're returning a String[], but your method says it should be returning a Set<String>.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 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?
- 04-11-2011, 12:56 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Spam reported
-
- 04-13-2011, 10:37 AM #7
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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
-
comparing the two files
By trkece in forum New To JavaReplies: 0Last Post: 02-21-2011, 10:27 AM -
Comparing two XML files
By anuj_sharma in forum XMLReplies: 1Last Post: 02-07-2011, 09:09 AM -
Comparing Two Text Files
By coder09 in forum New To JavaReplies: 15Last Post: 03-03-2009, 06:11 AM -
[SOLVED] Comparing two files
By Doctor Cactus in forum New To JavaReplies: 7Last Post: 02-27-2009, 10:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks