import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class LookStudent {
public void lookStudent() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\JavaSpace\\StudentManagers\\Student.txt"));
List<Student> list = new ArrayList<>();
Student stu = new Student();
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
System.out.println("学号为:" + split[0] + "姓名为:" + split[1] + "年龄为:" + split[2] + "地址为" + split[3]);
}
br.close();
}
}
这是查找学生类
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用学生管理系统");
System.out.println("1,添加学生");
System.out.println("2,查看学生");
System.out.println("3,删除学生");
System.out.println("4,修改学生信息");
System.out.println("5,退出本系统");
System.out.println("请选择您要进行的操作");
int work = sc.nextInt();
switch (work) {
case 1 -> addStudent();
case 2 -> {
LookStudent ls = new LookStudent();
ls.lookStudent();
}
case 3 -> deleteStudent();
case 4 -> changeStudent();
case 5 -> {
System.out.println("感谢您的使用");
System.exit(0);
}
}
}
}
private static void changeStudent() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\JavaSpace\\StudentManagers\\Student.txt"));
List<Student> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
Student stu = new Student();
String[] split = line.split(",");
stu.setId(Integer.parseInt(split[0]));
stu.setName(split[1]);
stu.setAge(Integer.parseInt(split[2]));
stu.setAddress(split[3]);
list.add(stu);
}
br.close();
System.out.println("请输入您要修改学生信息学生的学号");
int id = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) {
list.get(i).setId(id);
System.out.println("请输入学生的新名字");
list.get(i).setName(sc.next());
System.out.println("请输入学生的新年龄");
list.get(i).setAge(sc.nextInt());
System.out.println("请输入学生的新地址");
list.get(i).setAddress(sc.next());
} else {
System.out.println("查无学号,请从新输入");
}
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaSpace\\StudentManagers\\Student.txt"));
for (Student student : list) {
StringBuilder sb = new StringBuilder();
sb.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
private static void deleteStudent() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\JavaSpace\\StudentManagers\\Student.txt"));
List<Student> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
Student stu = new Student();
String[] split = line.split(",");
stu.setId(Integer.parseInt(split[0]));
stu.setName(split[1]);
stu.setAge(Integer.parseInt(split[2]));
stu.setAddress(split[3]);
list.add(stu);
}
br.close();
System.out.println("请输入您要删除的学生的学号");
int id = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) {
list.remove(i);
} else {
System.out.println("查无学号,请从新输入");
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaSpace\\StudentManagers\\Student.txt"));
for (Student student : list) {
StringBuilder sb = new StringBuilder();
sb.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
private static void addStudent() throws IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaSpace\\StudentManagers\\Student.txt", true));
List<Student> list = new ArrayList<>();
Student stu = new Student();
System.out.println("请输入学生学号");
stu.setId(sc.nextInt());
System.out.println("请输入学生姓名");
stu.setName(sc.next());
System.out.println("请输入学生年龄");
stu.setAge(sc.nextInt());
System.out.println("请输入学生住址");
stu.setAddress(sc.next());
list.add(stu);
for (Student student : list) {
String str = student.getId() + "," + student.getName() + "," + student.getAge() + "," + student.getAddress();
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}
}
主程序入口
public class Student {
private int Id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int id, String name, int age, String address) {
Id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
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;
}
@Override
public String toString() {
return "Student{" +
"Id=" + Id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
学生类