写入csv中的独立列

zhte4eai  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(415)

我试图写两个不同的数组到一个csv。我想在第一列中使用第一个数组,在第二列中使用第二个数组,如下所示:

array1val1   array2val1
array1val2   array2val2

我正在使用以下代码:

String userHomeFolder2 = System.getProperty("user.home") + "/Desktop";
String csvFile = (userHomeFolder2 + "/" + fileName.getText() + ".csv");
FileWriter writer = new FileWriter(csvFile);

final String NEW_LINE_SEPARATOR = "\n";
FileWriter fileWriter;
CSVPrinter csvFilePrinter;
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

fileWriter = new FileWriter(fileName.getText());
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

try (PrintWriter pw = new PrintWriter(csvFile)) {

    pw.printf("%s\n", FILE_HEADER);

    for(int z = 0; z < compSource.size(); z+=1) {
        //below forces the result to get stored in below variable as a String type

        String newStr=compSource.get(z);
        String newStr2 = compSource2.get(z);

        newStr.replaceAll(" ", "");
        newStr2.replaceAll(" ", "");

        String[] explode = newStr.split(",");
        String[] explode2 = newStr2.split(",");

        pw.printf("%s\n", explode, explode2);
    }
}
catch (Exception e) {
    System.out.println("Error in csvFileWriter");
    e.printStackTrace();

} finally {
    try {
        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();
    } catch (IOException e ) {
        System.out.println("Error while flushing/closing");
    }
}

但是,我在csv文件中得到了一个奇怪的输出:

[Ljava.lang.String;@17183ab4

我能跑

pw.printf("%s\n", explode);
pw.printf("%s\n", explode2);

而不是: pw.printf("%s\n", explode, explode2); 它打印实际的字符串,但都在同一列中。
有人知道怎么解决这个问题吗?

jxct1oxe

jxct1oxe1#

你的explode和explode2实际上是字符串数组。打印的是数组,而不是数组的值。所以你在最后得到数组的地址。你应该用循环遍历数组并打印出来。

for(int i = 0; i<explode.length;++i) {
pw.printf("%s%s\n", explode[i], explode2[i]);
}

2.方法printf应该是

pw.printf("%s%s\n", explode, explode2);

因为您正在打印两个参数,但在(“%s\n”,explode,explode2)中只打印了一个。
试一试,说它是否管用

e5njpo68

e5njpo682#

在这些行之后:

newStr.replaceAll(" ", "");
newStr2.replaceAll(" ", "");

String[] explode = newStr.split(",");
String[] explode2 = newStr2.split(",");

使用此代码:

int maxLength = Math.max(explode.length, explode2.length);
for (int i = 0; i < maxLength; i++) {
  String token1 = (i < explode.length) ? explode[i] : "";
  String token2 = (i < explode2.length) ? explode2[i] : "";

  pw.printf("%s %s\n", token1, token2);
}

这也涵盖了数组长度不同的情况。

4ioopgfo

4ioopgfo3#

我已经删除了所有未使用的变量,并对compsource的内容做了一些假设。此外,别忘了字符串是不可变的。如果只执行“newstr.replaceall(“,”“);”,替代品将丢失。

public class Tester {

    @Test
    public void test() throws IOException {

        // I assumed compSource and compSource2 are like bellow
        List<String> compSource = Arrays.asList("array1val1,array1val2");
        List<String> compSource2 = Arrays.asList("array2val1,array2val2");

        String userHomeFolder2 = System.getProperty("user.home") + "/Desktop";
        String csvFile = (userHomeFolder2 + "/test.csv");

        try (PrintWriter pw = new PrintWriter(csvFile)) {

            pw.printf("%s\n", "val1,val2");

            for (int z = 0; z < compSource.size(); z++) {

                String newStr = compSource.get(z);
                String newStr2 = compSource2.get(z);

                // String is immutable --> store the result otherwise it will be lost
                newStr = newStr.replaceAll(" ", "");
                newStr2 = newStr2.replaceAll(" ", "");

                String[] explode = newStr.split(",");
                String[] explode2 = newStr2.split(",");

                for (int k = 0; k < explode.length; k++) {
                    pw.println(explode[k] + "\t" + explode2[k]);
                }
            }
        }
    }
}

相关问题