Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-290

Produced CSV using PostgreSQL format cannot be read

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.6, 1.9.0
    • 1.10.0
    • Parser
    • None

    Description

      // code placeholder
      

      CSV, produced using printer:

       

      CSVPrinter printer = new CSVPrinter(sw, CSVFormat.POSTGRESQL_CSV.withFirstRecordAsHeader());

       

      cannot be be read with same format parser:

       

      CSVParser parser = new CSVParser(new StringReader(sw.toString()), CSVFormat.POSTGRESQL_CSV.withFirstRecordAsHeader());

       

      To reproduce: 

       

      StringWriter sw = new StringWriter(); 
      
      CSVPrinter printer = new CSVPrinter(sw, CSVFormat.POSTGRESQL_CSV.withFirstRecordAsHeader());  
      
      printer.printRecord("column1", "column2"); 
      printer.printRecord("v11", "v12"); 
      printer.printRecord("v21", "v22");  
      printer.close();  
      
      CSVParser parser = new CSVParser(new StringReader(sw.toString()), CSVFormat.POSTGRESQL_CSV.withFirstRecordAsHeader());  
      
      System.out.println("headers: " + Arrays.equals(parser.getHeaderNames().toArray(), new String[] {"column1", "column2"}));  
      
      Iterator<CSVRecord> i = parser.iterator(); 
      System.out.println("row: " + Arrays.equals(i.next().toList().toArray(), new String[] {"v11", "v12"})); 
      System.out.println("row: " + Arrays.equals(i.next().toList().toArray(), new String[] {"v21", "v22"}));

      I'd expect the above code to work, but it fails:

      java.io.IOException: (startline 1) EOF reached before encapsulated token finishedjava.io.IOException: (startline 1) EOF reached before encapsulated token finished 
      
      at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:371) 
      at org.apache.commons.csv.Lexer.nextToken(Lexer.java:285) 
      at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:701) 
      at org.apache.commons.csv.CSVParser.createHeaders(CSVParser.java:480) 
      at org.apache.commons.csv.CSVParser.<init>(CSVParser.java:432) 
      at org.apache.commons.csv.CSVParser.<init>(CSVParser.java:398) 
      at Test.main(Test.java:25)
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            artemana Anatoliy Artemenko
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1.5h
                1.5h