逐列写入CSV文件 [英] Write CSV file column-by-column

查看:57
本文介绍了逐列写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找答案,但没有找到.有没有人有解决此类问题的方法.我有一组文本变量,必须使用Java写入.CSV文件中.我目前正在使用需要Java的JavaScript进行项目.我现在拥有的此功能可以很好地完成工作,并将文本逐行写入.CSV.

I was searching for an answer for this but I didn't find it. Does anyone have a solution for this kind of problem. I have a set of text variables that I have to write into the .CSV file using Java. I am currently doing a project with JavaScript that calls for Java. This is a function that I have right now that does the job well and writes the text into .CSV line by line.

function writeFile(filename, data)
{
   try
   { 

      //write the data

      out = new java.io.BufferedWriter(new java.io.FileWriter(filename, true));
      out.newLine();
      out.write(data);
      out.close();
      out=null;
   }
   catch(e)   //catch and report any errors
   {
      alert(""+e);
   }
}

但是现在我必须像下面的例子一样逐一书写一部分文本.

But now I have to write parts of text one by one like the example bellow.

first0,second0,third0
first1,second1,third1
first2,second2,third2
.
.
.
first9,second9,third9

所以算法是这样的.该函数用逗号写入first0,然后转到下一行,写入first1,转到下一行,写入first2,依此类推,直到first9.完成该部分后,脚本将转到文件的开头,并在逗号后写入second0,转到下一行,并在逗号后写入second1,依此类推.你明白了.

So the algorithm goes like this. The function writes first0 with comma then goes to the next line writes first1, goes to next line writes first2 and so one until first9. After that part is done the script goes to the beginning of the file and writes second0 behind the comma, goes to the next line and writes second1 behind the comma and so on. You get the idea.

所以现在我需要java

So now I need java

推荐答案

您可能要考虑使用超级CSV 写入CSV文件.除了避免转义嵌入式双引号和逗号外,它还提供了一系列编写实现从数组/列表,地图甚至POJO编写的代码,这意味着您可以轻松地尝试一下自己的想法.

You might want to consider using Super CSV to write the CSV file. As well as taking care of escaping embedded double-quotes and commas, it offers a range of writing implementations that write from arrays/Lists, Maps or even POJOs, which means you can easily try out your ideas.

如果要使其非常简单,可以将CSV文件组装为二维数组.这样可以先将其组装成列,然后在准备就绪时将整个内容写入CSV.

If you wanted to keep it really simple, you can assemble your CSV file in a two-dimensional array. This allows to to assemble it column-first, and then write the whole thing to CSV when it's ready.

package example;

import java.io.FileWriter;
import java.io.IOException;

import org.supercsv.io.CsvListWriter;
import org.supercsv.io.ICsvListWriter;
import org.supercsv.prefs.CsvPreference;

public class ColumnFirst {

    public static void main(String[] args) {

        // you can assemble this 2D array however you want
        final String[][] csvMatrix = new String[3][3];
        csvMatrix[0][0] = "first0";
        csvMatrix[0][1] = "second0";
        csvMatrix[0][2] = "third0";
        csvMatrix[1][0] = "first1";
        csvMatrix[1][1] = "second1";
        csvMatrix[1][2] = "third1";
        csvMatrix[2][0] = "first2";
        csvMatrix[2][1] = "second2";
        csvMatrix[2][2] = "third2";

        writeCsv(csvMatrix);

    }

    private static void writeCsv(String[][] csvMatrix) {

        ICsvListWriter csvWriter = null;
        try {
            csvWriter = new CsvListWriter(new FileWriter("out.csv"), 
                CsvPreference.STANDARD_PREFERENCE);

            for (int i = 0; i < csvMatrix.length; i++) {
                csvWriter.write(csvMatrix[i]);
            }

        } catch (IOException e) {
            e.printStackTrace(); // TODO handle exception properly
        } finally {
            try {
                csvWriter.close();
            } catch (IOException e) {
            }
        }

    }

}

输出:

first0,second0,third0
first1,second1,third1
first2,second2,third2

这篇关于逐列写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆