Menu

#58 read only the first line

v1.0 (example)
closed
None
5
2018-09-28
2018-06-28
No

Hello,

i have to read the first line of the csv - file. The

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 1); ist marked as deprecated. How do i is now withe the opencsv-libary?

Thanks a lot for your help
Marcus

Related

Support Requests: #58

Discussion

  • Scott Conway

    Scott Conway - 2018-06-29

    Hello Marcus. Though you can still use that constructor for now we have moved to alot of the logic in the reader to a CSVParser class and then added an RFC4180Parser for those who absolutely needed RFC4180 compliance. Because we were adding so many new features instead of coming up with different constructors for all the combinations we created Builder classes. So for your above example where you have a single quote as the quote character and you want to skip a line before reading data (tab is default so not needed for the parser) you would do the following.

        /**
    
         * test building a CSVReader with a RFC4180Parser using the
         * appropriate builders
         *
         * @throws IOException But not really
         */
        @Test
        public void testSupportRequest58() throws IOException {
    
            StringBuilder sb = new StringBuilder(ICSVParser.INITIAL_READ_SIZE);
    
            sb.append("'This','line',should, skip").append("\n")
                    .append("a,'''',c").append("\n") // a,',c
                    .append("Second line,'Same as the first\nbut a whole lot longer',and a whole lot worse.").append("\n");
    
            RFC4180Parser parser = new RFC4180ParserBuilder().withQuoteChar('\'').build();
            CSVReader c = new CSVReaderBuilder(new StringReader(sb.toString()))
                    .withCSVParser(parser)
                    .withSkipLines(1)
                    .build();
    
    
            String[] nextLine = c.readNext();
            assertEquals(3, nextLine.length);
    
            assertEquals("a", nextLine[0]);
            assertEquals(1, nextLine[1].length());
            assertEquals("\'", nextLine[1]);
            assertEquals("c", nextLine[2]);
    
            nextLine = c.readNext();
            assertEquals(3, nextLine.length);
    
            assertEquals("Second line", nextLine[0]);
            assertEquals("Same as the first\nbut a whole lot longer", nextLine[1]);
            assertEquals("and a whole lot worse.", nextLine[2]);
        }
    

    That might seem like alot more but it makes it easier to configure the parser and reader exactly the way you want it.

    Hope that helps.

    Scott :)

     

    Last edit: Scott Conway 2018-07-02
  • Scott Conway

    Scott Conway - 2018-07-02

    Sorry I realized my code used separator instead of quote char. The withSkipLines is in the CSVReaderBuilder not the CSVReader. I changed my code example to a unit test that I see building and passing. Try that and see if that helps.

     
  • Andrew Rucker Jones

    • status: open --> closed
     
  • Andrew Rucker Jones

    Closed for lack of a response.

     

Log in to post a comment.