简体   繁体   English

如何转换清单 <String[]> Java中使用逗号分隔字符串(不带“”)

[英]How to Convert List<String[]> to Comma Separated String without “ ” in Java

I'm trying to remove fist line from the CSV file after reading it. 我试图在读取后从CSV文件中删除第一行。 Code is working properly but it's adding "" to my data when it's rewriting. 代码运行正常,但是在重写时在数据中添加了“”。 for an example: 例如:

  • before write to csv file: 100,COMRADE,CAMPBELL 写入csv文件之前:100,COMRADE,CAMPBELL
  • after write to csv file: "100","COMRADE","CAMPBELL" 写入csv文件后:“ 100”,“ COMRADE”,“ CAMPBELL”

    Here is the code. 这是代码。

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

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>

You can use another constructor when creating your writer. 创建作家时,可以使用其他构造函数。 Change 更改

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

to

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

this is the way to convert List to comma Seprated Single String. 这是将列表转换为逗号分隔的单个字符串的方法。

List<String[]> oldList= . . . ;
String requiredString = oldList
.stream()
    .map(Arrays::asList)
    .flatMap(Collection::stream)
    .collect(Collectors.joining(","));

Your CSV writer is writing each element as string in the csv file. 您的CSV编写器正在将每个元素作为字符串写入csv文件中。 This can be by design in library as it will keep any comma in your data in the string and later will not add extra column if your data has comma as well. 这可以通过库中的设计来完成,因为它将在字符串中的数据中保留任何逗号,如果您的数据中也包含逗号,以后将不会添加额外的列。

"name","class","house, street, city","value"

See the documentation of your library and check if it provides the option to not treat every element as string. 请参阅库的文档,并检查它是否提供了不将每个元素都视为字符串的选项。

But my opinion is to keep the strings in quotes to avoid data descripency during reads. 但是我的意见是将字符串保留在引号中,以避免在读取过程中出现数据描述问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM