(原创)JAVA-学生管理系统(最简单的SWING+IO读写文件持久化数据)详细代码)
需要源码的请加微信249278427,申请请备注好需要的源码标题,顺便点赞、评论、转发,谢谢!
一、先看项目运行效果:
管理员登录:
管理员主页:
新增:
查改删:
二、代码结构
三、代码(从上往下):
3.1 dao包
3.1.1 AdminDao
public class AdminDao {
//读取管理员数据
public Map<String, String> readAdmin() {
Map<String, String> map = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("admin.txt"))) {
String line = br.readLine();
String[] textData = null;
while (line != null) {
textData = line.split("\\,");
String name = textData[0];
String password = textData[1];
map.put(name, password);
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
3.1.2 StudentDao
public class StudentDao {
//读取学生数据
public ArrayList<Student> readStudent() {
ArrayList<Student> list = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("student.txt"))) {
String line = br.readLine();
String[] textData = null;
while (line != null) {
textData = line.split("\\,");
String name = textData[0];
String age = textData[1];
String address = textData[2];
String tel = textData[3];
Student student = new Student(0, name, age, address, tel);
list.add(student);
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//写入学生数据
public int writeStudent(Student student) {
ArrayList<Student> list = readStudent();
list.add(student);
int num = 0;
try {
FileWriter fileWriter = new FileWriter("student.txt");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
fileWriter.write(
stu.getName() + "," + stu.getAge() + "," + stu.getAddress() + "," + stu.getTel() + "\r\n");
}
fileWriter.flush();
fileWriter.close();
num = 1;
} catch (IOException e) {
e.printStackTrace();
}
return num;
}
//删除学生
public int delStudent(String name) {
ArrayList<Student> list = readStudent();
int num = 0;
for (int i = 0; i < list.size(); i++) {
if (name.equals(list.get(i).getName())) {
list.remove(i);
}
}
try {
FileWriter fileWriter = new FileWriter("student.txt");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
fileWriter.write(
stu.getName() + "," + stu.getAge() + "," + stu.getAddress() + "," + stu.getTel() + "\r\n");
}
fileWriter.flush();
fileWriter.close();
num = 1;
} catch (IOException e) {
e.printStackTrace();
}
return num;
}
//按名字查找学生
public ArrayList<Student> findStudent(String name) {
ArrayList<Student> list = readStudent();
ArrayList<Student> newlist = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().contains(name)){
newlist.add(list.get(i));
}
}
return newlist;
}
}
3.2 model
3.2.1 Admin
//管理员实体类
public class Admin {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Admin(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public Admin() {
super();
}
}
3.2.2 student
//学生实体类
public class Student {
private int id;
private String name; //姓名
private String age; //年龄
private String address; //地址
private String tel; //电话
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Student(int id, String name, String age, String address, String tel) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.tel = tel;
}
public Student() {
super();
}
}
3.3 util包
3.3.1 StringUtil
public class StringUtil {
//判空
public stati