java代码,宿舍管理系统的学生信息的导入导出功能
时间: 2024-03-12 22:46:32 浏览: 15
以下是一个简单的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 StudentInfoIO {
// 导入学生信息
public static List<Student> importStudentInfo(String filePath) throws IOException {
List<Student> students = new ArrayList<Student>();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
String[] items = line.split(",");
Student student = new Student(items[0], items[1], items[2], items[3]);
students.add(student);
}
reader.close();
return students;
}
// 导出学生信息
public static void exportStudentInfo(String filePath, List<Student> students) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
for (Student student : students) {
writer.write(student.toString());
writer.newLine();
}
writer.close();
}
}
```
在这个示例中,我们定义了一个 `Student` 类,用于表示一个学生的基本信息,包括姓名、学号、性别和班级。`importStudentInfo` 方法用于从指定的文件中读取学生信息,将其转换为 `Student` 对象列表并返回。`exportStudentInfo` 方法将一个 `Student` 对象列表写入到指定的文件中。
下面是 `Student` 类的代码实现:
```
public class Student {
private String name;
private String id;
private String gender;
private String clazz;
public Student(String name, String id, String gender, String clazz) {
this.name = name;
this.id = id;
this.gender = gender;
this.clazz = clazz;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getGender() {
return gender;
}
public String getClazz() {
return clazz;
}
public String toString() {
return name + "," + id + "," + gender + "," + clazz;
}
}
```
这是一个简单的实现,你可以根据实际需求进行修改和优化。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)