【问题标题】:Writing an array to a CSV File - Java将数组写入 CSV 文件 - Java
【发布时间】:2014-04-01 20:10:22
【问题描述】:

我希望将数组中的 24 个元素写入要在 Excel 中打开的 CSV 文件。

当我运行我的程序时,它会在我的 Netbeans 项目文件夹中创建一个 CSV 文件。但是,当我打开 CSV 文件时 - 它是空白的。

在我的 main 方法中,我向用户显示数组,然后调用 writeCSV 方法,如下所示:

    //show the user the sorted array
    System.out.println( "The sorted array is: ");
    for ( int i=0; i<24; i++)      
        System.out.println( "\t" + course2[i].toString() );

    //write the data from the duplicate array to a CSV file
    System.out.println("\nWriting data from Course array to CSV File.");
    writeCSV(course2, count);

writeCSV方法贴在下面:

    //write from duplicate array of courses to a CSV file
    public static void writeCSV(Course[] courseArray, int count) throws Exception {

    //create a File class object and give the file the name employees.csv
    java.io.File courseCSV = new java.io.File("courses.csv");

    //Create a Printwriter text output stream and link it to the CSV File
    java.io.PrintWriter outfile = new java.io.PrintWriter(courseCSV);

    //Iterate the elements actually being used
    for (int i=0; i < count ; i++) {
        outfile.write(courseArray[i].toCSVString());

    }//end for

    outfile.close();
    } //end writeCSV()

上面的 writeCSV 方法调用了我创建的名为 Course 的类中定义的 toCSVString 方法。我把这个方法贴在下面:

// method to return properties as a CSV string on one line
//public String toCSVString(Course c){
public String toCSVString() {
    String record = campus + ","
                  + course + ","
                  + section + ","
                  + crn + ","
                  + credits + ","
                  + time + ","
                  + days + "\n";

    return record;
} //end toCSVString()

我的代码运行完美,直到我必须将数组写入 CSV 文件。这是它创建空白 CSV 文件的时间。这让我相信我的 toCSVString 方法或我相信的 writeCSV 方法中有错误。任何提示或帮助将不胜感激。谢谢。

【问题讨论】:

  • 您确定计数设置正确而不是 0?这将与您的问题兼容。
  • 为什么要使用 count 等替代变量而不是数组的内置大小 courseArray.length?我将它视为一个函数参数,但想知道它的预期用途是否真的是您需要/想要的?
  • @Drunix 在我的 main 方法中,我首先定义 int count=0;
  • @RyanJ 我使用 writeCSV(course2,course2.length) 调用了 writeCSV 方法,它工作正常。谢谢!
  • ... 这解释了为什么您的文件为空(除非您稍后将其设置为正确的值)

标签: java arrays csv


【解决方案1】:

对于那些刚刚收听的人...

将您的 writeCSV 方法更改为:

//write from duplicate array of courses to a CSV file
public static void writeCSV(Course[] courseArray) throws Exception {

    //create a File class object and give the file the name employees.csv
    java.io.File courseCSV = new java.io.File("courses.csv");

    //Create a Printwriter text output stream and link it to the CSV File
    java.io.PrintWriter outfile = new java.io.PrintWriter(courseCSV);

    //Iterate the elements actually being used
    for (int i=0; i < courseArray.length ; i++) {
        outfile.write(courseArray[i].toCSVString());

    }//end for

    outfile.close();
} //end writeCSV()
  1. 从这个函数中移除 count 参数,除非你真的 打算将不同数量的元素写入 CSV 文件。 从你的主要方法来看,情况并非如此。

  2. 将 for 循环中的 count 更改为 courseArray.length

然后,在您的 main 方法中,将调用更改为:

writeCSV(course2);

始终确保初始化变量,如有疑问,请使用调试器。这本可以帮助您发现这一点。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-06-27
    • 2019-03-01
    • 2013-02-03
    • 2023-03-31
    • 2018-03-08
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    相关资源
    最近更新 更多