gpt4 book ai didi

java - 如何在 Java 中将 List 转换为不带 ""的逗号分隔字符串

转载 作者:行者123 更新时间:2023-12-02 10:07:42 25 4
gpt4 key购买 nike

我试图在阅读后从 CSV 文件中删除第一行。代码工作正常,但在重写时它在我的数据中添加了“”。举个例子:

  • 写入 csv 文件之前:100,COMRADE,CAMPBELL
  • 写入 csv 文件后:“100”、“COMRADE”、“CAMPBELL”

    这是代码。

================================================== =========================

public void deleteLineFromCsv() throws IOException {
List<PatientsTabData> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/patients.csv")));
String line = reader.readLine();
while(line != null) {
String[] split = line.split(",");
list.add(new PatientsTabData().patientId(Integer.parseInt(split[0])).patName(split[1]).patLastName(split[2]));
line = reader.readLine();
}
list.remove(0);
CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));
List<String[]> newlist = list.stream().map(item -> new String[]
{String.valueOf(item.getPatientId()), item.getPatName(), item.getPatLastName()})
.collect(Collectors.toList());
writer.writeAll(newlist);
writer.close();
}

Library:

        <dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>

最佳答案

您可以在创建编写器时使用另一个构造函数。改变

CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));

CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")),',',CSVWriter.NO_QUOTE_CHARACTER);

关于java - 如何在 Java 中将 List<String[]> 转换为不带 ""的逗号分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221124/

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