java练习:定义一个学生类 Student,包含属性name, age, id,性别,创高跑步公里数distance。设置每个属性的getter/setter方法,定义一个班级类,包含属性班级名称,学院等属性,班级的所有学生都存储在里面(49人)。-把班级班级成员写入要一个txt文件中,每个学生占一行,每个属性逗号分割。例如:姓名:张三,学号:111,...姓名:李四,学号:112,...-从txt文件重新读取然后把班级成员写入要一个csv文件中(Excel)
时间: 2024-03-14 18:43:50 浏览: 84
好的,下面是Java代码实现:
Student类:
```java
public class Student {
private String name;
private int age;
private String id;
private String gender;
private int distance;
public Student(String name, int age, String id, String gender, int distance) {
this.name = name;
this.age = age;
this.id = id;
this.gender = gender;
this.distance = distance;
}
// getter/setter methods
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 int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
}
```
Class类:
```java
import java.io.*;
public class Class {
private String className;
private String college;
private Student[] students;
public Class(String className, String college) {
this.className = className;
this.college = college;
this.students = new Student[49];
}
// getter/setter methods
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
// write students to txt file
public void writeStudentsToFile(String fileName) throws IOException {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
for (int i = 0; i < students.length; i++) {
Student student = students[i];
String line = "姓名:" + student.getName() + ",学号:" + student.getId() + ",年龄:" + student.getAge() + ",性别:" + student.getGender() + ",跑步公里数:" + student.getDistance();
printWriter.println(line);
}
printWriter.close();
fileWriter.close();
System.out.println("班级成员已写入文件" + fileName);
}
// read students from txt file and write to csv file
public void readStudentsFromFile(String txtFileName, String csvFileName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(txtFileName));
FileWriter fileWriter = new FileWriter(csvFileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
String line = bufferedReader.readLine();
while (line != null) {
String[] parts = line.split(",");
String name = parts[0].split(":")[1];
String id = parts[1].split(":")[1];
int age = Integer.parseInt(parts[2].split(":")[1]);
String gender = parts[3].split(":")[1];
int distance = Integer.parseInt(parts[4].split(":")[1]);
Student student = new Student(name, age, id, gender, distance);
students[students.length - 1] = student;
// write to csv file
printWriter.print(name + ",");
printWriter.print(age + ",");
printWriter.print(id + ",");
printWriter.print(gender + ",");
printWriter.println(distance);
line = bufferedReader.readLine();
}
printWriter.close();
fileWriter.close();
bufferedReader.close();
System.out.println("班级成员已从文件" + txtFileName + "中读取并写入文件" + csvFileName);
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) throws IOException {
Class cls = new Class("计算机科学与技术1班", "计算机学院");
// create 49 students
for (int i = 0; i < 49; i++) {
Student student = new Student("张三" + i, 20 + i % 5, "2021000" + String.format("%02d", i + 1), i % 2 == 0 ? "男" : "女", i * 100);
cls.getStudents()[i] = student;
}
// write to txt file
cls.writeStudentsToFile("students.txt");
// read from txt file and write to csv file
cls.readStudentsFromFile("students.txt", "students.csv");
}
}
```
运行后会在项目目录下生成students.txt和students.csv两个文件,分别是文本和CSV格式的班级成员信息。
阅读全文