gpt4 book ai didi

java - 在Java中正确地将对象数组输出到.csv文件

转载 作者:行者123 更新时间:2023-12-02 09:23:47 24 4
gpt4 key购买 nike

我正在努力完成这个项目,需要一些帮助。每当我尝试输出学生对象数组时,其数据字段为:

(Student ID, First name , Last name, Date of birth, Final mark, Grade).

我很确定这与我的 toString() 方法及其格式有关,也许我应该创建另一个用于输出格式的方法,而不是使用我想要的格式 toString()打印到屏幕。

我现在发布的这个问题的目标结果是让每行中的每个主题和每条数据(例如学生 ID、名字等)位于 .csv 文件或其他文件中的单独列中文字是 Excel 电子表格。

这是我的代码片段,如果有人可以帮助我找出一种或两种方法将对象数组中的信息正确输出到 .csv 文件,我将不胜感激!预先感谢您!

public class NewClass {
public static void main(String[] args) {
static PostGraduateStudent[] PGstudentArray;
}

private static void getPGStudentDetails() {

PostGraduateStudent s1 = new PostGraduateStudent();
s1.setName("Vasiliy", "Zaytsev");
s1.setDateOfBirth(02, 5, 1993);
s1.setID(25643924);
s1.setTitle("Mr");
s1.setEveryMark(50, 50, 50);
s1.calcMarks();
s1.calcGrade();
// System.out.println(s1);

PostGraduateStudent s2 = new PostGraduateStudent();
s2.setName("Joseph", "Stojk");
s2.setDateOfBirth(29, 12, 1990);
s2.setID(32454523);
s2.setTitle("Mr");
s2.setEveryMark(80, 70, 60);
s2.calcMarks();
s2.calcGrade();
// System.out.println(s2);

PostGraduateStudent s3 = new PostGraduateStudent();
s3.setName("Arkan", "Tigrovi");
s3.setDateOfBirth(23, 10, 1992);
s3.setID(83957293);
s3.setTitle("Mr");
s3.setEveryMark(60, 60, 70);
s3.calcMarks();
s3.calcGrade();
// System.out.println(s3);

PostGraduateStudent s4 = new PostGraduateStudent();
s4.setName("Draza", "Mihailovic");
s4.setDateOfBirth(23, 3, 1988);
s4.setID(13121389);
s4.setTitle("Mr");
s4.setEveryMark(99, 98, 99);
s4.calcMarks();
s4.calcGrade();
// System.out.println(s4);

PostGraduateStudent s5 = new PostGraduateStudent();
s5.setName("Dragana", "Mirkovic");
s5.setDateOfBirth(10, 9, 1979);
s5.setID(64926748);
s5.setTitle("Mr");
s5.setEveryMark(24, 26, 100);
s5.calcMarks();
s5.calcGrade();
//System.out.println(s5);

PostGraduateStudent s6 = new PostGraduateStudent();
s6.setName("Stefan", "Jokic");
s6.setDateOfBirth(10, 9, 1980);
s6.setID(4829464);
s6.setTitle("Mr");
s6.setEveryMark(98, 80, 98);
s6.calcMarks();
s6.calcGrade();

PGstudentArray = new PostGraduateStudent[] { s1, s2, s3, s4, s5, s6 };
}

private static void outputPGToFile() {
try {
FileOutputStream f = new FileOutputStream(new File("PostgradStudent.csv"));
ObjectOutputStream o = new ObjectOutputStream(f);

//Writes the objects to the file
for(int i = 0; i < PGstudentArray.length; i++) {
o.writeObject(PGstudentArray[i].toString());
}

o.close();
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initialising the stream");
}
}
}


//ToString function in my student class

@Override //Simple method to select format of output and output the instance variables
public String toString() {
String output = String.format("%5d %2d-%2d-%2d %-3s %-5s %-5s %3d %2s", this.studentID, this.day_dob, this.month_dob, this.year_dob, this.title, this.firstName, this.lastName, this.marks, this.grade);
return output;
}

这是我为研究生获得的输出示例。我无法发布图片,因为我没有 10 次代表,所以这里是它的链接:) https://gyazo.com/f3b1d23276607b0bb1edb890e5ddafef

最佳答案

虽然我在评论中建议您使用 BufferedWriter,但我认为使用 Files.write 更容易。

这样,您的 outputPGToFile() 将如下所示:

private static void outputPGToFile() {
try {
Files.write(Paths.get("PostgradStudent.csv"),
Arrays.stream(PGstudentArray).map(Object::toString).collect(Collectors.toList()),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}

(您需要添加适当的导入)

如果你确实需要使用旧的、低级的java.io东西,你可以使用这个:

private static void outputPGToFile() {
try (FileWriter fw = new FileWriter(new File("PostgradStudent.csv"));
BufferedWriter bw = new BufferedWriter(fw)) {
for (PostGraduateStudent student : PGstudentArray) {
bw.write(student.toString());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}

即使发生异常,try-with-resources也会确保FileWriterBufferedWriter都关闭。

关于java - 在Java中正确地将对象数组输出到.csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58484655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com