使用CSV序列化和反序列化
Posted xinyuan_java
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用CSV序列化和反序列化相关的知识,希望对你有一定的参考价值。
mvn 引入需要的jar包
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.0</version>
</dependency>
main sample
package org.cloud.avro;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class CsvMain
public static void main(String[] args)
try
read();
catch (Exception e)
e.printStackTrace();
public static void read() throws IOException
Reader reader = Files.newBufferedReader(Paths.get("D:\\\\file\\\\avro\\\\person.csv"));
CSVReader csvReader = new CSVReader(reader);
String[] next = csvReader.readNext();
while (next != null)
System.out.println(Arrays.toString(next));
next = csvReader.readNext();
csvReader.close();
public static void write() throws IOException
Writer writer = Files.newBufferedWriter(Paths.get("D:\\\\file\\\\avro\\\\person.csv"));
CSVWriter csvWriter = new CSVWriter(writer);
for (int i = 0; i < 10000000; i++)
Person person = new Person("identify" + i, "tom", 23);
csvWriter.writeNext(new String[] person.getId(), person.getName(), Objects.toString(person.getAge()));
csvWriter.flush();
csvWriter.close();
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Person
String id;
String name;
int age;
以上是关于使用CSV序列化和反序列化的主要内容,如果未能解决你的问题,请参考以下文章