java - 专门从一系列类(class)写入 csv 文件

标签 java arrays io

我在使用我编写的类中的方法时遇到问题。以下方法已添加到我创建的名为 Course 的类中。目标是将对象的所有属性作为字符串写在一行中。

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

   return record;
} //end toCSVString()

好的,所以在将该方法添加到类之后。然后我开始创建一个方法(从 main 方法调用)需要从 Course 数组写入调用上述方法的 CSV 文件。这是我写的方法。

// add a method here to write from an 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.toCSVString(courseArray[i]));

   }//end for

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

我在处理以“outfile.write”开头的行时遇到问题

在我的代码中,我无法让 Netbeans 找到在 Course 类中的 toString 方法之后定义的 toCSVString 方法。最初代码中的那一行看起来像这样:

outfile.write(toCSVString(courseArray[i]));

但是我的IDE找不到它所以我在它前面添加了类(class)对象的实例。但是,我仍然遇到问题。

有人看到我做错了什么吗?

编辑 #1

这是我程序中的 Course 类。我在使用 toCSVString 方法时遇到问题。

class Course implements Serializable {

   private String campus;  // the campus on which the course is offered
   private String course;  // the course number, such as CSCI 111
   private String section; // the section number
   private String crn;     // the CRN for this section
   private int credits;    // the number od credits for the course
   private String time;    // the time the course is offered, such as 8:00 to 10:00 A.M.
   private String days;    // the Days the course is offered, suhc as MW


   // constructors
Course() {
}

Course(String course, String section, String crn, int credits) {
    this.course = course;
    this.section = section;
    this.crn = crn;
    this.credits = credits;
}   // end Course() initalizing

// muatator methods
public void setCampus(String cmp) {
    this.campus = cmp;
}// end setCampus()

public void setCourse(String crse) {
    this.course = crse;
}// end setCourse()

public void setSection(String sect) {
    this.section = sect;
}   // end setSection()

public void setCRN(String crn) {
    this.crn  = crn;
}   // end setCRN()

public void setCredits(int cr) {
    this.credits = cr;
}   // end setCredits()

public void setTime(String tm) {
    this.time = tm;
}// end setTime()

public void setDays(String days) {
    this.days = days;
}// end setDays()




// accessor methods
public String getCampus() {
    return campus;
}   // end getCampus()

public String getCourse() {
    return course;
}   // end Course()

public String getSection() {
    return section;
}   // end getSection()

public String getCRN() {
    return crn;
}   // end getCRN()

public int getCredits() {
    return credits;
}   // end getCredits()

public String getTime() {
    return time;
}   // end getTime()

public String getDays() {
    return days;
}   // end getDays()


// method to compare by CRN using the String class compareTo()
public int compareTo(Course other) {
    return this.crn.compareTo(other.getCRN());
}   // end compareTO()

// method to return properties as a string
public String toString() {

    return campus + " "
            + course + " "
            + section + " "
            + crn + " "
            + credits + " "
            + time + " "
            + days;

}    // end toString()

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

    return record;
} //end toCSVString()


}// end class Course

最佳答案

你有:

outfile.write(courseArray.toCSVString(courseArray[i]));

你的意思是:

outfile.write(courseArray[i].toCSVString(courseArray[i]));

因为 toCSVStringCourse 的成员,而不是 Course[] 的成员(courseArray 是一个 Course[] 并且您正在尝试对数组本身调用 .toCSVString(),这是无效的。

另请注意,在这种形式中,将 Course 作为参数传递是多余的,因为您没有使用它并且您还需要 this 而不是一些无论如何,其他类(class)。我建议完全放弃该参数(因为它未被使用):

public String toCSVString () {      // <- c wasn't actually used
    String record = campus + ","    // <- and this. is implied here
                  + course + ","
                  + section + ","
                  + crn + ","
                  + credits + ","
                  + time + ","
                  + days + "\n";
    return record;
}

您只需将其称为:

outfile.write(courseArray[i].toCSVString());

或者,如果您愿意,可以将方法设为 static 并使用参数(尽管在这种情况下这不会给您带来任何特别的好处):

public static String toCSVString (Course c) {
    String record = c.campus + "," 
                  + c.course + ","
                  + c.section + ","
                  + c.crn + ","
                  + c.credits + ","
                  + c.time + ","
                  + c.days + "\n";
    return record;
}

如果您选择静态方法,那么您将其称为:

outfile.write(Course.toCSVString(courseArray[i]));

关于java - 专门从一系列类(class)写入 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596418/

相关文章:

php - 尝试从数组中删除重复的行。到栏目内容

java - Java中有参数树的实现吗?

scala - 从 Scala 中的同一源读取行和原始字节

java - 在 JavaFX SplitPane 上设置 OneTouchExpandable 按钮

java - 历史数组

java - 序列化器无法找到我的业务类并抛出 HazelcastSerializationException/ClassNotFoundException

linux - Linux下C语言如何获取文件长度?

java - 如何确保hibernate提交了事务?

c++ - std::array 存储位置如何?

java - java 中的堆空间要求 - Arrays.sort() 与 Collections.sort()