JAVA-学生管理系统(最简单的SWING+IO读写文件持久化数据)详细代码及步骤

(原创)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
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值