gpt4 book ai didi

java - OpenCSV parse() 返回具有空值的对象

转载 作者:行者123 更新时间:2023-12-05 07:31:28 25 4
gpt4 key购买 nike

我有这个输入 .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 文件但未填充数据。知道发生了什么事吗?

最佳答案

在我的例子中,我错误地实例化了 InputStreamReader

接收文件作为MultipartFile,我所做的只是new InputStreamReader(csvFile.getInputStream())

我会尝试将您的 reader 实例化更改为 FileReader new FileReader("yourfile.csv")

关于java - OpenCSV parse() 返回具有空值的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com