【问题标题】:Using OpenCSV to write in a column instead of row使用 OpenCSV 写入列而不是行
【发布时间】:2013-09-18 02:19:19
【问题描述】:

我有一个一维数组String[],我想将其写入 CSV 文件,然后在 Excel 等电子表格程序中查看。但是,此数组的大小/长度太大而无法容纳允许的列数,但足够小以容纳允许的行数。

那么,如何按列而不是按行编写?

我正在使用的当前代码:

import au.com.bytecode.opencsv.CSVWriter;

public class test {
    public static void main(String[] args) [
        CSVWriter writer2 = new CSVWriter(new FileWriter("testing.csv"), '\t');

        String[] string = new String[10];
        for(int i = 0; i < 10; i++) {
            string[i] = "Testing";
        }

        writer2.writeNext(string); //  !!EXPORTS AS ROW AND NOT A COLUMN!! //
        writer2.close();




    }
}

【问题讨论】:

  • @ppeterka 66 你为什么要更改我问题中的代码
  • 抱歉,我不是故意的……在编辑我的答案时,我一定是不小心点击了错误的编辑按钮……我把它恢复了。

标签: java csv text io opencsv


【解决方案1】:

要将String[] 写入文件,每个字符串在单独的行中,您不需要CSVWriter...普通的旧FileOutputStream 就可以了

public static class test {
  public static void main(String[] args) throws UnsupportedEncodingException, IOException {
    String[] strings = new String[10];
    for (int i = 0; i < 10; i++) {
      strings[i] = "Testing";
    }

    OutputStream output = new FileOutputStream("testing.csv");
    try {
      for (String s : strings) {
        output.write(s.getBytes("UTF-8")); // don't rely on default encoding!
      }
    }
    finally {
      output.close();
    }
  }
}

不过,如果你真的想用CSVWriter写:

public static class test {
  public static void main(String[] args) throws UnsupportedEncodingException, IOException {
    CSVWriter writer2 = new CSVWriter(new FileWriter("testing.csv"), '\t');

    String[] strings = new String[10];
    for (int i = 0; i < 10; i++) {
      strings[i] = "Testing";
    }

    String[] wrap = new String[1]; //probably saving on GC

    for (String s: strings) {
      wrap[0]=s;
      writer2.writeNext(wrap);
    }
    writer2.close();

  }

}

【讨论】:

    【解决方案2】:

    writeNext 写入 1 行。 如果你调用它两次,它会写 2 行,如果你调用它 10 次,它会写 10 行对吗? :)

    编辑:好的,让我们试试这个:

    public void writeLines(String[] array, CSVWriter writer) {
        for (final String oneElement : array) {
            writer.writeNext(new String[] {oneElement});
        }
    }
    

    现在你可以调用writeLines 方法来写一堆行了。如果我以你为例:

    import au.com.bytecode.opencsv.CSVWriter;
    
    public class test {
        public static void main(String[] args) {
            CSVWriter writer2 = new CSVWriter(new FileWriter("testing.csv"), '\t');
    
            String[] string = new String[10];
            for(int i = 0; i < 10; i++) {
                string[i] = "Testing";
            }
    
            writer2.writeLines(string); // This will write a column
            writer2.close();
        }
    }
    

    【讨论】:

    • 虽然这确实解决了我提供的玩具示例的问题,但我正在寻找一种方法来写一列,尽可能少写一行,以便它可以推广到更多复杂的问题。
    • 我认为您找不到像writer.writeColumn(...) 这样的简单方法。这些库经过优化,仅适用于线路流。完成添加列后,您必须缓冲数据创建(添加列)并写入所有内容(刷新)。
    • 我编辑了我的示例以向您展示如何编写专栏,但我不确定它是否是您所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    相关资源
    最近更新 更多