Created
January 25, 2022 19:45
-
-
Save mattnicee7/7cee5faa8842d96135cafc9f22c6fa93 to your computer and use it in GitHub Desktop.
Csv Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.mattnicee7; | |
import com.j256.simplecsv.common.CsvColumn; | |
import lombok.*; | |
@NoArgsConstructor | |
@AllArgsConstructor | |
@Getter | |
@Setter | |
@ToString | |
public class Account { | |
@CsvColumn(columnName = "Name") | |
private String name; | |
@CsvColumn(columnName = "Age") | |
private Integer age; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.mattnicee7; | |
import com.j256.simplecsv.processor.CsvProcessor; | |
import lombok.SneakyThrows; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Application { | |
private static final String CSV_FILE_PATH = "C://Testes/testes.csv"; | |
private static final CsvProcessor<Account> CSV_PROCESSOR = new CsvProcessor<>(Account.class); | |
public static void main(String[] args) { | |
final File csvFile = new File(CSV_FILE_PATH); | |
writeAll(csvFile, Arrays.asList( | |
new Account("Matheus", 18), | |
new Account("Silvio Santos", 95) | |
)); | |
readAll(csvFile).forEach(System.out::println); | |
} | |
@SneakyThrows | |
public static void writeAll(File file, List<Account> accountList) { | |
CSV_PROCESSOR.writeAll(file, accountList, true); | |
} | |
@SneakyThrows | |
public static List<Account> readAll(File file) { | |
return CSV_PROCESSOR.readAll(file, null); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<repositories> | |
<repository> | |
<id>central</id> | |
<name>Central Repository</name> | |
<url>https://repo.maven.apache.org/maven2</url> | |
<layout>default</layout> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>com.j256.simplecsv</groupId> | |
<artifactId>simplecsv</artifactId> | |
<version>2.6</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.18.22</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment