简体   繁体   中英

Parsing 2D array into CSV file using OpenCSV

I am trying to write a 2D array into a CSV file using OpenCSV API. I have the following method:

    Path path = Paths.get("/Users/Home/");
    String[][] d = new String[10][10];
    CSVWriter writer =
            new CSVWriter(Files.newBufferedWriter(path.resolve("data.csv"),StandardOpenOption.CREATE_NEW));
    writer.writeNext(Arrays.asList(d));  // Does not work!
    writer.close();

In fact, writer.writeNext(Arrays.asList(d)) does not work and I am getting stuck in this. Any help is appreciated.

From the docs of CSVWriter writeNext expects a String array. You could iterate as follows

for (int i=0; i < d.length; i++) {
   writer.writeNext(d[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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