opencsv csv 数据解析通用包
opencsv是一个很不错的java 版本csv 解析包,使用简单,而且比较灵活
参考代码
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>opencsv-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dalong.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- java code
package com.dalong;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.function.Consumer;
public class Application {
public static void main(String[] args) throws IOException {
try (InputStream inputStream = Application.class.getClassLoader().getResourceAsStream("demoapp.txt");) {
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
CsvToBean<Bank> csvToBean = new CsvToBeanBuilder(inputStreamReader).withType(Bank.class).withSeparator('|').withSkipLines(0).build();
csvToBean.parse().forEach(new Consumer<Bank>() {
public void accept(Bank bank) {
System.out.println(bank.toString());
}
});
inputStreamReader.close();
}
}
}
Bank.java
package com.dalong;
import com.opencsv.bean.CsvBindByPosition;
public class Bank {
@CsvBindByPosition(required = false,position = 0)
private String id;
public String getId() {
return id;
}
@Override
public String toString() {
return "Bank{" +
"id='" + id + '\'' +
", desc='" + desc + '\'' +
", name='" + name + '\'' +
'}';
}
public void setId(String id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@CsvBindByPosition(required = false,position = 1)
private String desc;
@CsvBindByPosition(required = false,position = 2)
private String name;
}
- 写入操作
package com.dalong;
import com.opencsv.CSVWriter;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import java.io.*;
import java.util.List;
import java.util.function.Consumer;
public class Application {
public static void main(String[] args) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
try (InputStream inputStream = Application.class.getClassLoader().getResourceAsStream("demoapp.txt");) {
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
CsvToBean<Bank> csvToBean = new CsvToBeanBuilder(inputStreamReader).withType(Bank.class).withSeparator('|').withSkipLines(1).build();
List<Bank> list = csvToBean.parse();
list.forEach(new Consumer<Bank>() {
public void accept(Bank bank) {
System.out.println(bank.toString());
}
});
inputStreamReader.close();
Writer writer = new FileWriter("yourfile.csv");
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).withSeparator('|').withQuotechar('\u0000').build();
beanToCsv.write(list);
writer.close();
}
}
}
说明
opencsv支持的操作还是比较多的,同时也支持sql 的处理,可以参考官方文档学习