Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting back to a csv with Java using super csv
    text
    copied!<p>Ive been working on this code for quite sometime and just want to be given the simple heads up if im routing down a dead end. The point where im at now is to mathch identical cells from diffrent .csv files and copy one row into another csv file. The question really is would it be possible to write at specfic lines say for example if the the 2 cells match at row 50 i wish to write back on to row 50. Im assuming that i would maybe extract everything to a hashmap, write it in there then write back to the .csv file? is there a easier way? </p> <p>for example i have one Csv that has person details, and the other has property details of where the actual person lives, i wish to copy the property details to the person csv, aswell as match them up with the correct person detail. hope this makes sense</p> <pre><code>public class Old { public static void main(String [] args) throws IOException { List&lt;String[]&gt; cols; List&lt;String[]&gt; cols1; int row =0; int count= 0; boolean b; CsvMapReader Reader = new CsvMapReader(new FileReader("file1.csv"), CsvPreference.EXCEL_PREFERENCE); CsvMapReader Reader2 = new CsvMapReader(new FileReader("file2.csv"), CsvPreference.EXCEL_PREFERENCE); try { cols = readFile("file1.csv"); cols1 = readFile("fiel2.csv"); String [] headers = Reader.getCSVHeader(true); headers = header(cols1,headers } catch (IOException e) { e.printStackTrace(); return; } for (int j =1; j&lt;cols.size();j++) //1 { for (int i=1;i&lt;cols1.size();i++){ if (cols.get(j)[0].equals(cols1.get(i)[0])) { } } } } private static List&lt;String[]&gt; readFile(String fileName) throws IOException { List&lt;String[]&gt; values = new ArrayList&lt;String[]&gt;(); Scanner s = new Scanner(new File(fileName)); while (s.hasNextLine()) { String line = s.nextLine(); values.add(line.split(",")); } return values; } public static void csvWriter (String fileName, String [] nameMapping ) throws FileNotFoundException { ICsvListWriter writer = new CsvListWriter(new PrintWriter(fileName),CsvPreference.STANDARD_PREFERENCE); try { writer.writeHeader(nameMapping); } catch (IOException e) { e.printStackTrace(); } } public static String[] header(List&lt;String[]&gt; cols1, String[] headers){ List&lt;String&gt; list = new ArrayList&lt;String&gt;(); String [] add; int count= 0; for (int i=0;i&lt;headers.length;i++){ list.add(headers[i]); } boolean c; c= true; while(c) { add = cols1.get(0); list.add(add[count]); if (cols1.get(0)[count].equals(null))// this line is never read errpr { c=false; break; } else count ++; } String[] array = new String[list.size()]; list.toArray(array); return array; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload