写字符串数组下在Java csv文件的列(writing an array of strings d

2019-10-17 02:03发布

所以我试图做到的是将数据写入阵列下来,我有尽可能多的数组元素列的每个单元格。 我想我也像这样在Excel中:

buyer | parts |  price| quantity  | comments
tom   | blah  | 546   | 5         |   blah
      |  blah | 56    |  67       |

我的文件中有比这更manyyyyy列,但我想给一个简单的例子。 什么也造成我的问题是,我不希望以后它打印下一列的最后一行,作者是曾对其做我的阵列数据集,所以我想这是对福斯特线。 我已研究过其他图书馆为实现这一点,但没有什么是我能找到,这似乎满足我的需求。 我明白CSV文件的性质可能不会允许这种情况发生,没有空白单元格中手动添加,但有没有其他的方法来做到这一点?

Enumeration <String> paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()) 
        {
            String paramName = (String)paramNames.nextElement();
                            writer.append(paramName);
            writer.append(',');
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1)
            {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                {
                    writer.append("No Value");
                    writer.append('\n');
                }
                else
                {
                    writer.append(paramValue);
                    writer.append('\n');
                }
            }
            else
            {
                for(int i = 0; i<paramValues.length; i++)
                {
                    writer.append(paramValues[i]);
                    writer.append(',');
                }
                writer.append('\n');
            }
            previousname = paramName;

        }
        writer.flush();
        writer.close();
    }

还阐明其中该数据是从,形式的中间的JSP表这就是即将到来,所以我在那些表中的值的阵列读取。

Answer 1:

来自: http://javacodeonline.blogspot.ca/2009/09/java-code-to-write-to-csv-file.html

/** The below Java Code writes 
 * some data to a file in CSV
 * (Comma Separated Value) File
 */

package developer;

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

public class MakeCSVFile {

    public static void main(String[] args) throws IOException {

        //Note the "\\" used in the path of the file 
        //instead of "\", this is required to read 
        //the path in the String format.
        FileWriter fw = new FileWriter("D:\\JavaCodeFile\\WriteTest.csv");
        PrintWriter pw = new PrintWriter(fw);

        //Write to file for the first row
        pw.print("Hello guys");
        pw.print(",");
        pw.print("Java Code Online is maing");
        pw.print(",");
        pw.println("a csv file and now a new line is going to come");

        //Write to file for the second row
        pw.print("Hey");
        pw.print(",");
        pw.print("It's a");
        pw.print(",");
        pw.print("New Line");

        //Flush the output to the file
        pw.flush();

        //Close the Print Writer
        pw.close();

        //Close the File Writer
        fw.close();        
    }
}

可能的,而不是做硬打印做阵列的一个循环,并打印由逗号分隔的每个值。

就像是 :

Array array;

    Iterator<String> iter = array.iterator();
    while (iter.hasNext())
    {
        object = iter.next();
        pw.print(object.getBuyer());
        pw.print(",");
        pw.print(object.getTitle());
        ..          
    }


Answer 2:

如果你正在写与Java的CSV文件,考虑使用OpenCSV库。



文章来源: writing an array of strings down a column of a csv file in java