CSVeed

Easy-to-use CSV to Java Bean utility

View project onGitHub

Typo's, number with the wrong decimal symbol, quotes in the wrong position, date formatting errors, column mismatches – if it wasn't for CSV, the life of us programmers might have been a lot easier. However, CSV is not to be ignored, side-tracked, stone-walled or dismissed. It's here and it's here to stay. The popularity of Excel has made it a de facto standard, so we might as well adjust and make the best of it.

Reading CSV files is an error-prone process, which is why CSVeed is Error Expectation Driven, doing its work in the full expectation that things will go wrong. And when things go wrong, CSVeed helps you to pinpoint the exact point of the error and reports this back you.

CSVeed is a Java library for reading Comma Separated Value (CSV) files and exposing those either as Rows or Java Beans.

Maven dependencies

In order to use CSVeed in your project, simply add the following dependency:

        <dependency>
            <groupId>com.github.hazendaz</groupId>
            <artifactId>csveed</artifactId>
            <version>0.7.0</version>
        </dependency>

For optimal usage, make sure you have a SLF4J configured. If you have no previous SLF4J logger configured and you want to get up and running quickly, use this:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>

Getting Started Real Quick

No time to waste, so here we go. Let us assume you have a Java Bean you want to convert your CSV rows into:

        import org.csveed.annotations.CsvDate;

        import java.util.Date;

        public class Bean {
            private String name;
            private Long number;
            @CsvDate(format="dd-MM-yyyy")
            private Date date;

            public String getName() { return name; }
            public Long getNumber() { return number; }
            public Date getDate() { return date; }
            public void setName(String name) { this.name = name; }
            public void setNumber(Long number) { this.number = number; }
            public void setDate(Date date) { this.date = date; }
        }

Note that the Date property has been annotated with @CsvDate, one of the CSVeed annotations. This allows CSVeed to convert the CSV text value into a java.util.Date using the date format in the annotation.

        Reader reader = new StringReader(
                "name;number;date\n"+
                "\"Alpha\";1900;\"13-07-1922\"\n"+
                "\"Beta\";1901;\"22-01-1943\"\n"+
                "\"Gamma\";1902;\"30-09-1978\""
        );
        
        CsvClient<Bean> csvReader = new CsvClientImpl<Bean>(reader, Bean.class);
        final List<Bean> beans = csvReader.readBeans();
        
        for (Bean bean : beans) {
            System.out.println(
                bean.getName()+" | " +
                bean.getNumber()+" | "+
                bean.getDate());
        }

It's that simple to get up and running. You could also opt to declare your instructions programmatically:

        Reader reader = new StringReader(
                "name;number;date\n"+
                "\"Alpha\";1900;\"13-07-1922\"\n"+
                "\"Beta\";1901;\"22-01-1943\"\n"+
                "\"Gamma\";1902;\"30-09-1978\""
        );
                
        CsvClient<Bean> csvReader = new CsvClientImpl<Bean>(reader,
                    new BeanReaderInstructionsImpl(Bean.class))
                .setMapper(ColumnNameMapper.class)
                .mapColumnNameToProperty("name", "name")
                .mapColumnNameToProperty("number", "number")
                .mapColumnNameToProperty("date", "date")
                .setDate("date", "dd-MM-yyyy");
        
        final List<Bean> beans = csvReader.readBeans();
        for (Bean bean : beans) {
            System.out.println(
                bean.getName()+" | " +
                bean.getNumber()+" | "+
                bean.getDate());
        }

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.