写入 csv 中的单独列

write to separate columns in csv

我正在尝试将 2 个不同的数组写入 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); 它打印实际的字符串,但都在同一列中。

有人知道怎么解决吗?

1.Your explode 和 explode2 实际上是字符串数组。您正在打印数组而不是它的值。所以你在最后得到打印数组的地址。 您应该使用循环遍历数组并将它们打印出来。

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

2.Also 方法 printf 应该类似于

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

因为你正在打印两个参数,但在 ("%s\n", explode, explode2) 中只打印了一个。

试试看是否有效

在这些行之后:

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);
}

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

我删除了所有未使用的变量并对 compSource 的内容做了一些假设。 此外,不要忘记 String 是不可变的。如果只做"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]);
                }
            }
        }
    }
}