写入csv中的单独列

时间:2017-07-14 15:33:42

标签: java csv apache-commons

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

有谁知道如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

1.您的爆炸和爆炸2实际上是字符串数组。您正在打印数组而不是它的值。所以你最后得到阵列的ADRESS。 你应该通过循环遍历数组并打印出来。

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

因为你要打印两个参数,但在(&#34;%s \ n&#34;,explode,explode2)中只打印了一个。

尝试一下并说它是否有效

答案 1 :(得分:0)

在以下几行之后:

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

这也涵盖了阵列长度不同的情况。

答案 2 :(得分:0)

我删除了所有未使用的变量,并对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]);
                }
            }
        }
    }
}