title: Write data in CSV with Java
body: <p>I'm trying to write data in a new CSV generated automatically after reading and extracting my data. However, always write one record only and it hasn't got the format that I want. I want that the data in columns, I want that if I have id, manufacturer, product_name, price, this information should be in diferent columns but my result is:</p>
  <p><a href="https://i.stack.imgur.com/1cLUQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1cLUQ.png" alt="enter code here" /></a></p>
  <p>Nevertheless in other script MDB developed for other person, when transform this CSV his format is:</p>
  <pre><code>51645A-C    11.86   21  INFOWORK    CARTUCHO COMPATIBLE CON HP 45 51645A NEGRO  
  http://recursos.infowork.es/img/000/084/000084322.jpg
  C13T04014020-C  1.01    3   INFOWORK    CARTUCHO COMPATIBLE CON EPSON C62-CX3200 NEGRO  http://recursos.infowork.es/img/000/084/000084593.jpg
  </code></pre>
  <p>I need this format with Java, to have my process automatically update my DB. Why? Now, I'm using one script for transformation and after I need to re-save result.</p>
  <p>My actual code is:</p>
  <pre><code>public static void main(String[] args) throws FileNotFoundException, IOException, CsvValidationException, CsvException {
      Path archCSV = Paths.get(System.getProperty(&quot;user.dir&quot;), &quot;MYLAR.csv&quot;);
      
  
      CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
      try (BufferedReader br = Files.newBufferedReader(archCSV,  StandardCharsets.UTF_8);
                                
          CSVReader reader = new CSVReaderBuilder(br).withCSVParser(parser).build()) {
          
          String col1 = &quot;&quot;;
          String col2 = &quot;&quot;;
          String col3 = &quot;&quot;;
          String col4 = &quot;&quot;;
          String col5 = &quot;&quot;;
          String col6 = &quot;&quot;;
          String col7 = &quot;&quot;;
          String col8 = &quot;&quot;;
          
          // Skip HTTP Headers
          for (String line; (line = br.readLine()) != null; )
              if (line.isEmpty())
                  break; // Found end of HTTP Headers
         
              String[] fila = null;
              while((fila = reader.readNext()) != null) {
                  col1 = fila[0]; //path
                  col2 = fila[1]; //fabricante
                  col3 = fila[2]; //ID_PRODUCT
                  col4 = fila[3]; // producto
                  col5 = fila[4]; //referencia
                  col6 = fila[6]; //stock
                  col7 = fila[7]; //EAN13
                  col8 = fila[9]; //PRECIO
                  
                  File file = new File(&quot;MYLAR2.CSV&quot;); 
                  try { 
                      // create FileWriter object with file as parameter 
                      FileWriter outputfile = new FileWriter(file); 
  
                      // create CSVWriter object filewriter object as parameter 
                      CSVWriter writer = new CSVWriter(outputfile); 
  
                      // adding header to csv 
                      String[] header = { &quot;Fabricante&quot;, &quot;ID_PRODUCT&quot;, &quot;PRODUCTO&quot;, &quot;REFERENCIA&quot;, &quot;STOCK&quot;, &quot;EAN13&quot;, &quot;PRECIO&quot;}; 
                      writer.writeNext(header); 
                      String[] data1 = new String[fila.length];
                      // add data to csv 
                      for(int i = 1; i&lt;fila.length; i++){
                          data1[i] = fila[i];
                      }
                      writer.writeNext(data1); 
                      
                      /*String[] data2 = { &quot;Suraj&quot;, &quot;10&quot;, &quot;630&quot; }; 
                      writer.writeNext(data2); */
  
                      // closing writer connection 
                      writer.close(); 
                  } 
                  catch (IOException e) { 
                      // TODO Auto-generated catch block 
                      e.printStackTrace(); 
                  } 
                  
                  // code here
              }
          }
  }
  </code></pre>
  <p>And result for transformation I wrote has only one result, when CSV has many results.</p>
  <p><strong>UPDATE</strong></p>
  <p><em>Original File Mylar</em></p>
  <pre><code>    HTTP/1.1 200 OK
  Date: Wed, 04 Nov 2020 10:33:57 GMT
  Server: Apache/2.4.18 (Unix) OpenSSL/1.0.2g PHP/5.6.19 mod_perl/2.0.8-dev Perl/v5.16.3
  Vary: Host
  X-Powered-By: PHP/5.6.19
  Pragma: public
  Last-Modified: Wed, 04 Nov 2020 10:35:38 GMT
  Cache-Control: no-store, no-cache, must-revalidate
  Cache-Control: pre-check=0, post-check=0, max-age=0
  Cache-Control: private
  Content-Transfer-Encoding: none
  Content-Disposition: attachment; filename=&quot;tarifa_general.csv&quot;
  Content-Length: 1682234
  Set-Cookie: 6bc890cb7b5704cff62e4f70e6f2fe91=xjqG7DAXpIyCS%2FBGEFwnApzYUWiwlkMqmx9ULY%2FdgW%2FMqfHk4mNo67rgSQp10%2FacRlHCinJUyWAv0B%2FInmAGaiyJ0ro5yf2yNl1O9tKx50M%3D000074; expires=Tue, 24-Nov-2020 10:33:57 GMT; Max-Age=1727899; path=/; domain=www.mylar.es; httponly
  Content-Type: text/csv; charset=utf-8
  
  CATEGORY_PATH;MANUFACTURER;ID_PRODUCT;PRODUCT;REFERENCE;REFERENCE_PARENT;STOCK;EAN13;SHORT_DESCRIPTION;PRICE_CUSTOMER
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;1;&quot;IMPRESORA EPSON LQ-2190&quot;;C11CA92001;;0;8715946459172;000003;910.33
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;3;&quot;IMPRESORA EPSON LX-1170 II&quot;;C11C641001;;0;8715946316963;000008;428.91
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;4;&quot;IMPRESORA EPSON FX-890 II&quot;;C11C524025;;0;8715946333137;000012;595.29
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;5;&quot;IMPRESORA EPSON FX-2190&quot;;C11C526022;;0;8715946204420;000013;714.87
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;6;&quot;IMPRESORA EPSON LQ-680 PRO&quot;;C11C376125;;0;8715946333205;000021;664.98
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;8;&quot;IMPRESORA EPSON LQ-630&quot;;C11C480019;;0;8715946538068;000025;498.67
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;11;&quot;IMPRESORA EPSON LQ-690&quot;;C11CA13041;;0;8715946429939;000091;583.8
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;12;&quot;IMPRESORA EPSON LX-350&quot;;C11CC24031;;0;8715946502939;000092;171.23
  &quot;PERIFÉRICOS &gt; SCANNERS &gt; EPSON&quot;;EPSON;17;&quot;SCANNER EPSON WORKFORCE DS-7500N&quot;;B11B205331BT;;0;8715946510705;000157;1063.95
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; ACCESORIOS&quot;;EPSON;18;&quot;TRACTOR PUSH/PULL ( FX-2190 )  C12C800212*&quot;;C12C800212;;0;0;000230;59.54
  &quot;PERIFÉRICOS &gt; TERMINAL PUNTO VENTA (TPV) &gt; IMPRESORAS TICKETS&quot;;EPSON;19;&quot;IMPRESORA EPSON TMU-950 PARALELO BLANCA&quot;;C31C176252LG;;0;8715946367842;000248;512.8
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; LASER&quot;;EPSON;21;&quot;IMPRESORA EPSON ACULASER COLOR C2800N&quot;;C11CA09001BZ;;0;8715946403878;000251;294.51
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; LASER&quot;;EPSON;22;&quot;IMPRESORA EPSON EPL-6200N&quot;;C11C533011BR;;0;9314020599675;000254;168.41
  &quot;PERIFÉRICOS &gt; SCANNERS &gt; EPSON&quot;;EPSON;24;&quot;SCANNER EPSON PERFECTION V500 OFFICE ADF&quot;;B11B189081;;0;8715946448664;000257;194.32
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; LASER&quot;;EPSON;25;&quot;IMPRESORA EPSON ACULASER M 7000N&quot;;C11CB61011;;0;8715946311173;000264;0
  &quot;PERIFÉRICOS &gt; SCANNERS &gt; EPSON&quot;;EPSON;26;&quot;SCANNER EPSON GT-S55&quot;;B11B209301BT;;0;8715946490922;000266;381.92
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; EPSON &gt; MATRICIALES&quot;;EPSON;51;&quot;IMPRESORA EPSON LQ-350&quot;;C11CC25001;;0;8715946521886;000854;188.01
  &quot;CONSUMIBLES &gt; ALMACENAMIENTO &gt; MEMORIAS USB / PENDRIVES&quot;;KINGSTON;68;&quot;PENDRIVE KINGSTON 32GB  USB 2.0&quot;;32GB;;0;740617297737;001439;3.29
  &quot;CONSUMIBLES &gt; ALMACENAMIENTO &gt; MEMORIAS USB / PENDRIVES&quot;;;77;&quot;PENDRIVE INTEGRAL 64GB* USB 2.0&quot;;&quot;INFD64GBPASBLS  PEN64GB&quot;;;0;5055288427822;001460;7.34
  &quot;CONSUMIBLES &gt; ALMACENAMIENTO &gt; MEMORIAS USB / PENDRIVES&quot;;TOSHIBA;79;&quot;PENDRIVE TOSHIBA  16GB  USB 2.0&quot;;&quot;THN U202&quot;;;0;4047999400226;001498;3.91
  &quot;PERIFÉRICOS &gt; IMPRESORAS / FAX &gt; IMPRESORAS &gt; BROTHER &gt; ACCESORIOS&quot;;BROTHER;81;&quot;DISCO DURO BROTHER 10 GB PARA  HL4000CN&quot;;HD40CL;;0;4977766603645;001677;411.4
  </code></pre>
  <p>the result with script to other developed with mdb script is:</p>
  <pre><code>C11CA92001  910.33  0   MYLAR   IMPRESORA EPSON LQ-2190
  C11C641001  428.91  0   MYLAR   IMPRESORA EPSON LX-1170 II
  C11C524025  595.29  0   MYLAR   IMPRESORA EPSON FX-890 II
  C11C526022  714.87  0   MYLAR   IMPRESORA EPSON FX-2190
  C11C376125  664.98  0   MYLAR   IMPRESORA EPSON LQ-680 PRO
  C11C480019  498.67  0   MYLAR   IMPRESORA EPSON LQ-630
  C11CA13041  583.8   0   MYLAR   IMPRESORA EPSON LQ-690
  C11CC24031  171.23  0   MYLAR   IMPRESORA EPSON LX-350
  B11B205331BT    1063.95 0   MYLAR   SCANNER EPSON WORKFORCE DS-7500N
  C12C800212  59.54   0   MYLAR   TRACTOR PUSH/PULL ( FX-2190 )  C12C800212*
  C31C176252LG    512.8   0   MYLAR   IMPRESORA EPSON TMU-950 PARALELO BLANCA
  C11CA09001BZ    294.51  0   MYLAR   IMPRESORA EPSON ACULASER COLOR C2800N
  C11C533011BR    168.41  0   MYLAR   IMPRESORA EPSON EPL-6200N
  B11B189081  194.32  0   MYLAR   SCANNER EPSON PERFECTION V500 OFFICE ADF
  C11CB61011  0   0   MYLAR   IMPRESORA EPSON ACULASER M 7000N
  B11B209301BT    381.92  0   MYLAR   SCANNER EPSON GT-S55
  C11CC25001  188.01  0   MYLAR   IMPRESORA EPSON LQ-350
  32GB    3.29    0   MYLAR   PENDRIVE KINGSTON 32GB  USB 2.0
  INFD64GBPASBLS  PEN64GB 7.34    0   MYLAR   PENDRIVE INTEGRAL 64GB* USB 2.0
  THN U202    3.91    0   MYLAR   PENDRIVE TOSHIBA  16GB  USB 2.0
  HD40CL  411.4   0   MYLAR   DISCO DURO BROTHER 10 GB PARA  HL4000CN
  FAX2845 159.2   0   MYLAR   FAX BROTHER 2845 LASER CON AURICULAR
  </code></pre>
  <p><strong>I need this format (up)</strong></p>
  <p><strong>With my actual code result is:</strong></p>
  <pre><code>    Fabricante,&quot;ID_PRODUCT&quot;,&quot;PRODUCTO&quot;,&quot;REFERENCIA&quot;,&quot;STOCK&quot;,&quot;EAN13&quot;,&quot;PRECIO&quot;
   ,&quot;MANUFACTURER&quot;,&quot;ID_PRODUCT&quot;,&quot;PRODUCT&quot;,&quot;REFERENCE&quot;,&quot;REFERENCE_PARENT&quot;,&quot;STOCK&quot;,&quot;EAN13&quot;,&quot;SHORT_DESCRIPTION&quot;,&quot;PRICE_CUSTOMER&quot;
      ,&quot;EPSON&quot;,&quot;1&quot;,&quot;IMPRESORA EPSON LQ-2190&quot;,&quot;C11CA92001&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946459172&quot;,&quot;000003&quot;,&quot;910.33&quot;
      ,&quot;EPSON&quot;,&quot;3&quot;,&quot;IMPRESORA EPSON LX-1170 II&quot;,&quot;C11C641001&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946316963&quot;,&quot;000008&quot;,&quot;428.91&quot;
      ,&quot;EPSON&quot;,&quot;4&quot;,&quot;IMPRESORA EPSON FX-890 II&quot;,&quot;C11C524025&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946333137&quot;,&quot;000012&quot;,&quot;595.29&quot;
      ,&quot;EPSON&quot;,&quot;5&quot;,&quot;IMPRESORA EPSON FX-2190&quot;,&quot;C11C526022&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946204420&quot;,&quot;000013&quot;,&quot;714.87&quot;
      ,&quot;EPSON&quot;,&quot;6&quot;,&quot;IMPRESORA EPSON LQ-680 PRO&quot;,&quot;C11C376125&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946333205&quot;,&quot;000021&quot;,&quot;664.98&quot;
      ,&quot;EPSON&quot;,&quot;8&quot;,&quot;IMPRESORA EPSON LQ-630&quot;,&quot;C11C480019&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946538068&quot;,&quot;000025&quot;,&quot;498.67&quot;
      ,&quot;EPSON&quot;,&quot;11&quot;,&quot;IMPRESORA EPSON LQ-690&quot;,&quot;C11CA13041&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946429939&quot;,&quot;000091&quot;,&quot;583.8&quot;
      ,&quot;EPSON&quot;,&quot;12&quot;,&quot;IMPRESORA EPSON LX-350&quot;,&quot;C11CC24031&quot;,&quot;&quot;,&quot;0&quot;,&quot;8715946502939&quot;,&quot;000092&quot;,&quot;171.23&quot;
  </code></pre>
  <p><strong>Explanation</strong></p>
  <p>i can´t pass the complete CSV file of a company distributor, I have passed some example lines that with that should give an idea of ​​what the whole file is like. Anyway, I'll try to make it clearer ... The first example after the update is my original mylar file. There was a former developer who made a script with MDB that transformed it as seen in the first example, which is the original from the distributor to the second example ... and finally with my current java script I get the third example. I need to put myself as the second example. I have terms in Spanish and English because this is how the CSVs of the providers come ... What I need to do is an automatic update of a database, with these CSVs, but for this, I have to transform them previously obtaining the data that is there in my script, but keep the CSV as in the example of the MDB script</p>