栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的用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 list = new ArrayList();
		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 list = new ArrayList();
		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 list = new ArrayList();
		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 list = new ArrayList();
		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 list = new ArrayList();
		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("没有此学号,请重新输入");
			}
		}
	}

	
	@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 {

	
	public static void readStudent(String path, List 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();
		}
	}

	
	public static void writeData(String path, List 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();
		}
	}

	
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号