Я решил, используя
import com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
вместо
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
И используя
import org.apache.commons.collections4.comparators.FixedOrderComparato4r;
`
И создав Hashmap для сопоставления CSV Заголовки данных начинаются с заглавных букв для атрибутов поля Бина.
// Hashmap to map CSV data to
// Bean attributes.
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("code_regio", "CODE_REGIO");
mapping.put("etrangers", "Etrangers");
mapping.put("marocains", "Marocains");
mapping.put("menages", "Menages");
mapping.put("nom_region", "Nom_Region");
mapping.put("population", "Population");
mapping.put("ruleid", "RuleID");
mapping.put("shape__are", "Shape__Are");
mapping.put("shape__len", "Shape__Len");
// Creating HeaderColumnNameMappingStrategy
HeaderColumnNameTranslateMappingStrategy<Properties> mappingStrategy = new HeaderColumnNameTranslateMappingStrategy<Properties>();
mappingStrategy.setType(Properties.class);
mappingStrategy.setColumnMapping(mapping);
// Setting predefined order using String comparator
mappingStrategy.setColumnOrderOnWrite(new FixedOrderComparator<>(Properties.FIELDS_ORDER));
String csv = CsvUtils.convertToCsv(localBusinessTrainingPairsDTO, mappingStrategy);
System.out.println(csv);
writer.close();
И в свойствах класса я прописал все заглавные буквы FIELDS_ORDER
public static final String[] FIELDS_ORDER = { "CODE_REGIO",
"ETRANGERS",
"MAROCAINS",
"MENAGES",
"NOM_REGION",
"POPULATION",
"RULEID",
"SHAPE__ARE",
"SHAPE__LEN" };
Таким образом, класс свойств бина сопоставления становится таким, как показано ниже
import com.opencsv.bean.CsvBindByName;
public class Properties {
public static String[] getFieldsOrder() {
return FIELDS_ORDER;
}
public String getCODE_REGIO() {
return CODE_REGIO;
}
public void setCODE_REGIO(String cODE_REGIO) {
CODE_REGIO = cODE_REGIO;
}
public int getPopulation() {
return Population;
}
public void setPopulation(int population) {
Population = population;
}
public int getMenages() {
return Menages;
}
public void setMenages(int menages) {
Menages = menages;
}
public int getEtrangers() {
return Etrangers;
}
public void setEtrangers(int etrangers) {
Etrangers = etrangers;
}
public int getMarocains() {
return Marocains;
}
public void setMarocains(int marocains) {
Marocains = marocains;
}
public String getNom_Region() {
return Nom_Region;
}
public void setNom_Region(String nom_Region) {
Nom_Region = nom_Region;
}
public int getRuleID() {
return RuleID;
}
public void setRuleID(int ruleID) {
RuleID = ruleID;
}
public double getShape__Are() {
return Shape__Are;
}
public void setShape__Are(double shape__Are) {
Shape__Are = shape__Are;
}
public double getShape__Len() {
return Shape__Len;
}
public void setShape__Len(double shape__Len) {
Shape__Len = shape__Len;
}
@CsvBindByName(column = "CODE_REGIO", required = true)
private String CODE_REGIO;
@CsvBindByName(column = "Etrangers", required = true)
private int Etrangers;
@CsvBindByName(column = "Marocains", required = true)
private int Marocains;
@CsvBindByName(column = "Menages", required = true)
private int Menages;
@CsvBindByName(column = "Nom_Region", required = true)
private String Nom_Region;
@CsvBindByName(column = "Population", required = true)
private int Population;
@CsvBindByName(column = "RuleID", required = true)
private int RuleID;
@CsvBindByName(column = "Shape__Are", required = true)
private double Shape__Are;
@CsvBindByName(column = "Shape__Len", required = true)
private double Shape__Len;
public static final String[] FIELDS_ORDER = { "CODE_REGIO",
"ETRANGERS",
"MAROCAINS",
"MENAGES",
"NOM_REGION",
"POPULATION",
"RULEID",
"SHAPE__ARE",
"SHAPE__LEN" };
}