我正在尝试使用opencsv 4.6 api创建csv文件。我能够使用标题需要在csv文件中的顺序序列来创建csv。但是生成的标题名称是大写字符。我希望它类似于我提到的映射值。
output getting :
"_ID","_NAME","_VISIBILITY","_TYPE","_PRICE"
"1","Tata","true","petrol","52642"
"2","Mercedes","false","diesel","57127"
"3","Skoda","true","petrol","9000"
"4","Volvo","true","petrol","29000"
output expected :
"_id","_name","_visibility","_type","_price"
"1","Tata","true","petrol","52642"
"2","Mercedes","false","diesel","57127"
"3","Skoda","true","petrol","9000"
"4","Volvo","true","petrol","29000"
这是我用来创建csv的bean类。
import com.opencsv.bean.CsvBindByName;
public class Car {
public static final String[] FIELDS_ORDER = { "_id", "_name", "_visibility", "_type", "_price" };
@CsvBindByName(column = "_id" )
private int id;
@CsvBindByName(column = "_name")
private String name;
@CsvBindByName(column = "_price")
private int price;
@CsvBindByName(column = "_visibility")
private boolean visibility;
@CsvBindByName(column = "_type")
private String type;
public Car(int id, String name, int price, boolean visibility, String type) {
this.id = id;
this.name = name;
this.price = price;
this.visibility = visibility;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isVisibility() {
return visibility;
}
public void setVisibility(boolean visibility) {
this.visibility = visibility;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Car{id=").append(id).append(", name=").append(name).append(", price=").append(price)
.append("}");
return builder.toString();
}
}
这是我正在创建的主要课程
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class A {
public static void main(String[] args)
throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
List<Car> cars = new ArrayList<>();
cars.add(new Car(1, "Tata", 52642, true, "petrol"));
cars.add(new Car(2, "Mercedes", 57127, false, "diesel"));
cars.add(new Car(3, "Skoda", 9000, true, "petrol"));
cars.add(new Car(4, "Volvo", 29000, true, "petrol"));
Writer writer = new FileWriter("yourfile.csv");
HeaderColumnNameMappingStrategy<Car> strategy = new HeaderColumnNameMappingStrategy<>();
strategy.setType(Car.class);
strategy.setColumnOrderOnWrite(new OrderedComparatorIgnoringCase(Car.FIELDS_ORDER));
StatefulBeanToCsv<Car> beanToCsv = new StatefulBeanToCsvBuilder<Car>(writer)
.withMappingStrategy(strategy).build();
beanToCsv.write(cars);
writer.close();
System.out.println("Done");
}
}
这是用于设置订单的代码,即我们要获取数据的顺序。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class OrderedComparatorIgnoringCase implements Comparator<String> {
private List<String> predefinedOrder;
public OrderedComparatorIgnoringCase(String[] predefinedOrder) {
this.predefinedOrder = new ArrayList<>();
for (String item : predefinedOrder) {
this.predefinedOrder.add(item.toLowerCase());
}
}
@Override
public int compare(String o1, String o2) {
return predefinedOrder.indexOf(o1.toLowerCase()) - predefinedOrder.indexOf(o2.toLowerCase());
}
}