java用到try...catch方法 定义一个学生类Student,包含属性name, age, id,性别,创高跑步公里数distance。设置每个属性的getter/setter方法 定义一个班级类,包含属性班级名称,学院等属性,班级的所有学生都存储在里面(49人)。 -把班级班级成员写入要- -个tbxt文件中, 每个学生占一行,每个属性逗号分割 例如: 姓名:张三,学号:111... 姓名:李四,学号:112... -从txt文件重新读取然后把班级成员写入要- 个csv文件中(Excel)
时间: 2024-03-17 10:46:05 浏览: 103
好的,我会尽力回答。首先,这是一个Java编程的问题,需要用到文件读写、类的定义、属性和方法的设置等知识点。下面是我给出的代码实现,其中包含了注释解析。
```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// 学生类
class Student {
private String name;
private int age;
private String id;
private String gender;
private double distance;
public Student() {}
public Student(String name, int age, String id, String gender, double distance) {
this.name = name;
this.age = age;
this.id = id;
this.gender = gender;
this.distance = distance;
}
// getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
// toString方法,用于输出学生信息
public String toString() {
return "姓名:" + name + ", 年龄:" + age + ", 学号:" + id + ", 性别:" + gender + ", 跑步公里数:" + distance;
}
}
// 班级类
class Class {
private String name;
private String college;
private List<Student> students;
public Class() {}
public Class(String name, String college) {
this.name = name;
this.college = college;
this.students = new ArrayList<Student>();
}
// 添加学生
public void addStudent(Student student) {
students.add(student);
}
// 将班级成员写入txt文件中
public void writeToFile(String filename) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Student student : students) {
writer.write(student.getName() + "," + student.getAge() + "," + student.getId() + "," + student.getGender() + "," + student.getDistance() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("写入文件失败:" + e.getMessage());
}
}
// 从txt文件中读取班级成员
public void readFromFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
String name = fields[0];
int age = Integer.parseInt(fields[1]);
String id = fields[2];
String gender = fields[3];
double distance = Double.parseDouble(fields[4]);
Student student = new Student(name, age, id, gender, distance);
students.add(student);
}
reader.close();
} catch (IOException e) {
System.out.println("读取文件失败:" + e.getMessage());
}
}
// 将班级成员写入csv文件中
public void writeToCsv(String filename) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write("姓名,年龄,学号,性别,跑步公里数\n");
for (Student student : students) {
writer.write(student.getName() + "," + student.getAge() + "," + student.getId() + "," + student.getGender() + "," + student.getDistance() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("写入文件失败:" + e.getMessage());
}
}
}
public class Test {
public static void main(String[] args) {
// 创建班级对象
Class clazz = new Class("一班", "计算机科学与技术学院");
// 添加学生
clazz.addStudent(new Student("张三", 20, "20190101", "男", 10.5));
clazz.addStudent(new Student("李四", 19, "20190102", "女", 8.3));
// 将班级成员写入txt文件中
clazz.writeToFile("class.txt");
// 从txt文件中读取班级成员
clazz.readFromFile("class.txt");
// 将班级成员写入csv文件中
clazz.writeToCsv("class.csv");
}
}
```
在代码中,我们首先定义了一个`Student`类,包含了姓名、年龄、学号、性别、跑步公里数等属性,以及对应的getter和setter方法。然后定义了一个`Class`类,包含了班级名称、学院等属性和学生列表,以及添加学生、将班级成员写入txt文件、从txt文件中读取班级成员、将班级成员写入csv文件等方法。最后,在`main`函数中创建了一个班级对象,并添加了两个学生,然后将班级成员写入txt文件、从txt文件中读取班级成员、将班级成员写入csv文件。
注意,为了方便起见,这里只添加了两个学生,实际情况中需要添加所有学生。另外,文件的写入和读取操作需要用到异常处理(try...catch语句),以防止程序出错导致程序崩溃。
阅读全文