我有这个.csv输入文件(这里只是一个示例-有1.500.000行)
OBJECT_ID¦TYPE¦NAME¦PARENT_ID¦MANUFACTURER¦POSITION_CODE¦MAINTENANCE_STATUS¦PHYSICAL_STATUS
9143101068113202824¦Card¦H603CSRI - 6¦9143101069013360101¦Huawei Technologies¦74EX.G010.R13.031 (H603).006¦Not on Maintenance¦In Service
9143101068113202825¦Card¦H603CSRI - 7¦9143101069013360101¦Huawei Technologies¦74EX.G010.R13.031 (H603).007¦Not on Maintenance¦In Service
我创建了一个名为Card的模型类
public class Card {
@CsvBindByName(column = "OBJECT_ID")
private String object_id;
@CsvBindByName(column = "TYPE")
private String type;
@CsvBindByName(column = "NAME")
private String name;
@CsvBindByName(column = "PARENT_ID")
private String parent_id;
@CsvBindByName(column = "MANUFACTURER")
private String manufacturer;
@CsvBindByName(column = "POSITION_CODE")
private String position_code;
@CsvBindByName(column = "MAINTENANCE_STATUS")
private String maintenance_status;
@CsvBindByName(column = "PHYSICAL_STATUS")
private String physical_status;
public Card() { }
public Card(String object_id, String type, String name, String parent_id, String manufacturer, String position_code, String maintenance_status, String physical_status) {
this.object_id = object_id;
this.type = type;
this.name = name;
this.parent_id = parent_id;
this.manufacturer = manufacturer;
this.position_code = position_code;
this.maintenance_status = maintenance_status;
this.physical_status = physical_status;
}
public String getObject_id() {
return object_id;
}
public void setObject_id(String object_id) {
this.object_id = object_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParent_id() {
return parent_id;
}
public void setParent_id(String parent_id) {
this.parent_id = parent_id;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getPosition_code() {
return position_code;
}
public void setPosition_code(String position_code) {
this.position_code = position_code;
}
public String getMaintenance_status() {
return maintenance_status;
}
public void setMaintenance_status(String maintenance_status) {
this.maintenance_status = maintenance_status;
}
public String getPhysical_status() {
return physical_status;
}
public void setPhysical_status(String physical_status) {
this.physical_status = physical_status;
}
}
我有这种方法应将所有csv内容加载到cards List
中,然后将数据放入Map
public static Map<String, Card> getAllCards(String fileLocation) {
log.logInfo("getAllCards started!");
long totalTime = System.currentTimeMillis();
Map<String, Card> map = new HashMap<>();
try (
Reader reader = Files.newBufferedReader(Paths.get(fileLocation))
) {
CsvToBean<Card> csvToBean = new CsvToBeanBuilder(reader)
.withSeparator('¦')
.withType(Card.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<Card> cards = csvToBean.parse();
System.out.println(cards.size());//1.535.232
for(Card card: cards) {
Card cardObj = new Card(card.getObject_id(),card.getType(),card.getName(), card.getParent_id(),
card.getManufacturer(), card.getPosition_code(), card.getMaintenance_status(),card.getPhysical_status());
map.put(card.getObject_id(), cardObj);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(map.get("OBJECT_ID"));//null
System.out.println(map.size());//1 as it inserts only one null key
log.logInfo("getAllCards completed in " + ((double) (System.currentTimeMillis() - totalTime) / 1000) + " sec.");
return map;
}
问题在于卡列表包含具有所有空值的对象的列表。大小正确,因此它正在读取csv文件,但未填充数据。知道发生了什么吗?
答案 0 :(得分:0)
就我而言,我错误地实例化了 InputStreamReader
。
接收文件为 MultipartFile
,我所做的只是 new InputStreamReader(csvFile.getInputStream())
。
我会尝试将您的 reader
实例更改为 FileReader new FileReader("yourfile.csv")
。