java代码,宿舍管理系统的学生信息的导入导出功能
时间: 2024-03-11 19:48:03 浏览: 28
以下是一个简单的 Java 代码示例,用于实现宿舍管理系统的学生信息导入导出功能。此示例使用 CSV 文件格式作为数据交换格式,您可以根据需要更改代码以支持其他格式。
```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;
public class StudentManager {
private List<Student> students = new ArrayList<>();
public void importStudents(String filename) throws IOException {
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 gender = fields[2];
String dormitory = fields[3];
Student student = new Student(name, age, gender, dormitory);
students.add(student);
}
}
}
public void exportStudents(String filename) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
for (Student student : students) {
String line = String.format("%s,%d,%s,%s", student.getName(), student.getAge(), student.getGender(), student.getDormitory());
writer.write(line);
writer.newLine();
}
}
}
}
class Student {
private String name;
private int age;
private String gender;
private String dormitory;
public Student(String name, int age, String gender, String dormitory) {
this.name = name;
this.age = age;
this.gender = gender;
this.dormitory = dormitory;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDormitory() {
return dormitory;
}
}
```
以上代码定义了一个 `StudentManager` 类,它维护一个学生列表,并提供了 `importStudents` 和 `exportStudents` 方法来导入和导出学生信息。学生信息被表示为 `Student` 类的实例,包含学生的姓名、年龄、性别和宿舍信息。`importStudents` 方法从 CSV 文件中读取学生信息,`exportStudents` 方法将学生信息写入 CSV 文件中。您可以调用这些方法来实现宿舍管理系统中的学生信息导入导出功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)