简单的用IO流写学生后台管理系统

利用java语言,用IO传输数据的方式,写一个学生后台管理系统,数据需要持久化保存在本地磁盘上。(仅留底)

包com.gao

类StudentManagerMain

package com.gao;

import java.io.File;
import java.io.IOException;

import com.gao.controller.StudentHandler;


public class StudentManagerMain {

	public static void main(String[] agrs) throws IOException {

		String path = "D:/student.txt";
		File file=new File(path);
		file.createNewFile();
		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("6.退出系统");
			System.out.println("");
			String choice = StudentHandler.input("请输入你的选择");
			switch (choice) {
			case "1":
				StudentHandler.queryAllStudent(path);
				break;
			case "2":
				StudentHandler.addStudent(path);
				break;
			case "3":
				StudentHandler.removeStudent(path);
				break;
			case "4":
				StudentHandler.updateStudent(path);
				break;
			case "5":
				StudentHandler.queryOneStudent(path);
				break;
			case "6":
			default:
				System.out.println("谢谢使用");
				System.exit(0);
			}
		}
	}
}

包com.gao.bean

类Student

package com.gao.bean;

public class Student {

	// 学号
	private String uid;

	// 姓名
	private String name;

	// 年龄
	private String age;

	// 地址ַ
	private String adress;

	public Student() {

	}

	public Student(String uid, String name, String age, String adress) {
		super();
		this.uid = uid;
		this.name = name;
		this.age = age;
		this.adress = adress;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUid() {
		return uid;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getAge() {
		return age;
	}

	public String getAdress() {
		return adress;
	}

	public void setAdress(String adress) {
		this.adress = adress;
	}

	@Override
	public String toString() {
		return uid + "," + name + "," + age + "," + adress;
	}

}

包com.gao.controller

类StudentHandler

package com.gao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.gao.bean.Student;
import com.gao.modules.StudentFile;

public class StudentHandler {

	/**
	 * 查询全部学生信息
	 */
	public static void queryAllStudent(String path) {
		List<Student> list = new ArrayList<Student>();
		StudentFile.readStudent(path, list);
		if (list.size() == 0) {
			System.out.println("没有学生信息");
			return;
		}
		System.out.println("学号\t姓名\t年龄\t住址");
		for (int i = 0; i < list.size(); i++) {
			Student stu = list.get(i);
			System.out.println(stu.getUid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAdress());
		}
	}

	/**
	 * 按照学号查询
	 */
	public static void queryOneStudent(String path) {

		List<Student> list = new ArrayList<Student>();
		StudentFile.readStudent(path, list);
		String id = input("请输入你要查询的学生学号:");
		for (int i = 0; i < list.size(); i++) {
			Student stu = list.get(i);
			if (stu.getUid().equals(id) == true) {
				System.out.println("学号\t姓名\t年龄\t住址");
				System.out.println(stu.getUid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAdress());
			}
		}
	}

	/**
	 * 录入学生信息
	 */
	public static void addStudent(String path) {
		List<Student> list = new ArrayList<Student>();
		StudentFile.readStudent(path, list);
		boolean flag = false;
		String id = input("请输入学号:");
		while (true) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getUid().equals(id)) {
					flag = true;
					break;
				} else {
					flag = false;
				}
			}
			if (flag) {
				System.out.println("学号重复,请重新输入");
			} else {
				break;
			}
		}
		String name = input("请输入姓名:");
		String age = input("请输入年龄:");
		String adr = input("请输入地址:");
		Student stu = new Student();
		stu.setUid(id);
		stu.setName(name);
		stu.setAge(age);
		stu.setAdress(adr);
		list.add(stu);
		StudentFile.writeData(path, list);
		System.out.println("添加成功");

	}

	/**
	 * 修改学生信息
	 */
	public static void updateStudent(String path) {

		List<Student> list = new ArrayList<Student>();
		StudentFile.readStudent(path, list);
		int index = -1;
		while (true) {
			String id = input("请输入学号:");
			for (int i = 0; i < list.size(); i++) {
				Student stu = list.get(i);
				if (stu.getUid().equals(id)) {
					index = i;
					break;
				}
			}
			if (index >= 0) {
				String name = input("请输入姓名:");
				String age = input("请输入年龄:");
				String adr = input("请输入地址:");
				Student stu = new Student();
				stu.setUid(id);
				stu.setName(name);
				stu.setAge(age);
				stu.setAdress(adr);
				list.set(index, stu);
				StudentFile.writeData(path, list);
				System.out.println("修改成功");
				break;
			} else {
				System.out.println("没有此学号,请重新输入");
			}
		}
	}

	/**
	 * 移除学生信息
	 */
	public static void removeStudent(String path) {
		List<Student> list = new ArrayList<Student>();
		StudentFile.readStudent(path, list);
		while (true) {
			String id = input("请输入你要删除的学生学号:");
			int index = -1;
			for (int i = 0; i < list.size(); i++) {
				Student stu = list.get(i);
				if (stu.getUid().equals(id)) {
					index = i;
					break;
				}
			}
			if (index >= 0) {
				list.remove(index);
				StudentFile.writeData(path, list);
				System.out.println("删除成功");
				break;
			} else {
				System.out.println("没有此学号,请重新输入");
			}
		}
	}

	/**
	 * 输入控制
	 * 
	 * @param msg
	 * @return
	 */
	@SuppressWarnings("resource")
	public static String input(String msg) {
		Scanner sc = new Scanner(System.in);
		System.out.println(msg);
		return sc.nextLine();
	}

}

包com.gao.modules

类StudentFile

package com.gao.modules;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.gao.bean.Student;
import com.gao.modules.StudentFile;

public class StudentFile {

	/**
	 * 从控制台获取学生信息进行添加到文件
	 * 
	 * @param path
	 * @param list
	 */
	public static void readStudent(String path, List<Student> list) {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader(path));
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				String[] str = line.split(",");
				Student student = new Student();
				student.setUid(str[0]);
				student.setName(str[1]);
				student.setAge(str[2]);
				student.setAdress(str[3]);
				list.add(student);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 从文件中读取信息到控制台
	 * 
	 * @param path
	 * @param list
	 */
	public static void writeData(String path, List<Student> list) {
		BufferedWriter bufferedWriter = null;
		try {
			bufferedWriter = new BufferedWriter(new FileWriter(path));
			for (int i = 0; i < list.size(); i++) {
				Student student = list.get(i);
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(student.getUid() + "," + student.getName() + "," + student.getAge() + ","
						+ student.getAdress());
				bufferedWriter.write(stringBuilder.toString());
				bufferedWriter.newLine();
				bufferedWriter.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			bufferedWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
}

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

555015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值