Read CSV in Java (Super CSV Annotation)

pom.xml


		<dependency>
			<groupId>com.github.mygreen</groupId>
			<artifactId>super-csv-annotation</artifactId>
			<version>2.2</version>
		</dependency>

XXXCsvBean.java


import com.github.mygreen.supercsv.annotation.CsvBean;
import com.github.mygreen.supercsv.annotation.CsvColumn;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@CsvBean
@NoArgsConstructor
@AllArgsConstructor
@Data
public class XXXCsvBean {
    @CsvColumn(number = 1)
    private String user;

    @CsvColumn(number = 2)
    private String email;
}

XXX.java


                try (CsvAnnotationBeanReader<XXXCsvBean> csvReader = getCsvReader(inputStream)) {
                    csvReader.getHeader(true); //Header line skip
                    XXXCsvBean readRecord;
                    while ((readRecord = csvReader.read()) != null) {
                        String user = readRecord.getUser();
                        // do something
                    }
                }

--

    private CsvAnnotationBeanReader<XXXCsvBean> getCsvReader(InputStream inputStream) {
        return new CsvAnnotationBeanReader<>(XXXCsvBean.class,
                new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)),
                CsvPreference.STANDARD_PREFERENCE);
    }

Recommended Posts

Read CSV in Java (Super CSV Annotation)
Read binary files in Java 1
Read standard input in Java
Read binary files in Java 2
[Java] Annotation
Easily read text files in Java (Java 11 & Java 7)
Read Java properties file in C #
[Java] Annotation
I made an annotation in Java.
Call the super method in Java
Read items containing commas in a CSV file without splitting (Java)
Read Felica using RC-S380 (PaSoRi) in Java
Read xlsx file in Java with Selenium
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Read a string in a PDF file with Java
Try scraping about 30 lines in Java (CSV output)
Deserialize CSV in Java based on header name
Add, read, and delete Excel comments in Java
[Java] Read the file in src / main / resources
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Read the packet capture obtained by tcpdump in Java
"Hello World" in Java
Read Java Property file
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Read Java HashMap source
Hello World in Java
Use OpenCV in Java
About Java Polymorphism super ()
webApi memorandum in java
History of Java annotation
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
When you want to dynamically replace Annotation in Java8
Java CSV library "opencsv"
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java