## 利用java的IO流知识编写简单的学生管理系统

刚学到IO流的知识,便根据所学编写出常用的学生管理系统。实现登录、注册、从文件中通过IO流读写数据的方式对数据进行增删改查。

package com.auto.test;
/**

  • 将数据存储在文## 标题件中
  • 从文件中逐行读取数据,先放入ArrayList中,再从ArrayList中读取Student对象
  • 在写入数据时,先将Student中的数据存入ArrayList中,再将其数据拼接成字符串存入文件中
  • 即 文件(String)-------->ArrayList-------->Student对象
  • Student对象--------->ArrayList-------->文件(String)

*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {
	private static final String filepath="dome.txt";
	/**
	 * main  主要用来控制主界面
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		while(true){
			System.out.println("请输入相应的功能:1-登`在这里插入代码片`录2-注册3-退出");
			Scanner sc=new Scanner(System.in);
			int num=sc.nextInt();
			if(num==1){
				if(login(filepath)){
					while(true){
						System.out.println("请输入相应的功能:1-添加学生信息2-修改学生信息3-删除学生信息"
								+ "4-查询学生信息5-查询学生信息表6-退出");
						int type=sc.nextInt();
						if(type==1){
							addStudent(filepath);
						}
						if(type==2){
							update(filepath);			
									}
						if(type==3){
							deleteStu(filepath);
						}
						if(type==4){
							selectStu(filepath);
						}
						if(type==5){
							selectStudents(filepath);
						}
						if(type==6){
							System.out.println("欢迎再次查询");
							break;
						}
					}
				}
				else{
					continue;
				}
				
			}
			if(num==2){
				register(filepath);
						}
			if(num==3){
				System.out.println("欢迎再次使用");
				break;
			}
		}
		
		
	}
	/**
	 * 登录代码
	 * @param filepath
	 * @return
	 * @throws IOException
	 */
	public static boolean login(String filepath) throws IOException{
		String id=InputStu.InputStr("请输入学生学号");
		ArrayList<Student> list=read(filepath);
		for(int i=0;i<list.size();i++){
			Student stus=list.get(i);
			if(stus.getId().equals(id)==true){
				System.out.println("登录成功");
				return true;
			}
		}
		System.out.println("请注册");
		return false;
	}
	/**
	 * 注册方法
	 * @param filepath
	 * @throws IOException
	 */
	public static void register(String filepath) throws IOException{
		addStudent(filepath);
		System.out.print("可登录");
	}
	/**
	 * 添加学生信息
	 * @param filepath
	 * @throws IOException
	 */
	public static void addStudent(String filepath) throws IOException{
		String id=InputStu.InputStr("请输入学生学号");
		String name=InputStu.InputStr("请输入学生姓名");
		int age=InputStu.InputInt("请输入学生年龄");
		String address=InputStu.InputStr("请输入学生住址");
		String phone=InputStu.InputStr("请输入学生手机号");
		String info=InputStu.InputStr("请输入学生相关信息");
		Student stu=new Student(id,name,age,address,phone,info);
		ArrayList<Student> li=new ArrayList<Student>();
		li.add(stu);
		ArrayList<Student> list=read(filepath);
		if(list.size()==0){
			writer(filepath,li);
			System.out.println("添加成功");
			return;
		}else{
			for(int i=0;i<list.size();i++){
				Student stus=list.get(i);
				//System.out.println("进入");
				if(li!=null&&id.equals(stus.getId())==false){
					writer(filepath,li);
					System.out.println("添加成功");
					return;
				}
			}
		}
	}
	/**
	 * 查询学生列表
	 * @param filepath
	 * @throws IOException
	 */
	public static void selectStudents(String filepath) throws IOException{
		ArrayList<Student> list=read(filepath);
		for(int i=0;i<list.size();i++){
			Student stu=list.get(i);
			System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()
			+","+stu.getAddress()+","+stu.getPhone()+","+stu.getOtherInfo());
		}
		
	}
	/**
	 * 查询某个学生信息
	 * @param filepath
	 * @throws IOException
	 */
	public static void selectStu(String filepath) throws IOException{
		String id=InputStu.InputStr("请输入要查询的学生学号");
		ArrayList<Student> list=read(filepath);
		for(int i=0;i<list.size();i++){
			Student stu=list.get(i);
			if(stu.getId().equals(id)==true){
				System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge()
				+","+stu.getAddress()+","+stu.getPhone()+","+stu.getOtherInfo());
				return;
			}
		}
	}
	/**
	 * 修改学生信息
	 * @param filapath
	 * @throws IOException
	 */
	public static void update(String filapath) throws IOException{
		String id=InputStu.InputStr("请输入要修改的学生学号");
		ArrayList<Student> list=read(filepath);
		int index=-1;
		for(int i=0;i<list.size();i++){
			Student stus=list.get(i);
			if(stus.getId().equals(id)==true){
				index=i;
				String name=InputStu.InputStr("请输入学生姓名");
				int age=InputStu.InputInt("请输入学生年龄");
				String address=InputStu.InputStr("请输入学生住址");
				String phone=InputStu.InputStr("请输入学生手机号");
				String info=InputStu.InputStr("请输入学生相关信息");
				list.get(i).setId(id);
				list.get(i).setName(name);
				list.get(i).setAddress(address);
				list.get(i).setAge(age);
				list.get(i).setPhone(phone);
				list.get(i).setOtherInfo(info);
				
			}
		}
		if(index==-1){
			System.out.println("该学生不存在,请重新输入学号");
		}else{
			//写入更改后的list数据,覆盖掉原来的数据
			FileOutputStream fos=new FileOutputStream(filepath);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			PrintWriter pw=new PrintWriter(osw,true);
			for(int k=0;k<list.size();k++){
				Student stu=list.get(k);
				pw.println(stu.getId()+","+stu.getName()+","+stu.getAge()
				+","+stu.getAddress()+","+stu.getPhone()+","+stu.getOtherInfo());	
			}
			System.out.println("修改成功");
			pw.close();
		}
	}
	/**
	 * 删除学生信息
	 * @param filepath
	 * @throws IOException
	 */
	public static void deleteStu(String filepath) throws IOException{
		String id=InputStu.InputStr("请输入要删除的学生学号");
		ArrayList<Student> list=read(filepath);
		int index=-1;
		for(int i=0;i<list.size();i++){
			Student stus=list.get(i);
			if(stus.getId().equals(id)==true){
				index=i;
				break;
			}
		}
		if(index==-1){
			System.out.println("查无此人,请重新输入学号");
			return;
		}else{
			list.remove(index);
			//写入更改后的list数据,覆盖掉原来的数据
			FileOutputStream fos=new FileOutputStream(filepath);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			PrintWriter pw=new PrintWriter(osw,true);
			for(int i=0;i<list.size();i++){
				Student stu=list.get(i);
				pw.println(stu.getId()+","+stu.getName()+","+stu.getAge()
				+","+stu.getAddress()+","+stu.getPhone()+","+stu.getOtherInfo());
			}
			System.out.println("删除成功");
			pw.close();
			
			
		}
	}

	
	/**
	 * 将文件中的内容读入list中
	 * @param filepath
	 * @return
	 * @throws IOException
	 */
	public static ArrayList<Student> read(String filepath) throws IOException{
		FileInputStream fis=new FileInputStream(filepath);
		InputStreamReader ir=new InputStreamReader(fis);
		BufferedReader br=new BufferedReader(ir);
		ArrayList<Student> list=new ArrayList<Student>();
		String str=null;
		while((str=br.readLine())!=null){
			String[] strs=str.split(",");
			Student stu=new Student();
			stu.setId(strs[0]);
			stu.setName(strs[1]);
			stu.setAge(Integer.parseInt(strs[2]));
			stu.setAddress(strs[3]);
			stu.setPhone(strs[4]);
			stu.setOtherInfo(strs[5]);
			list.add(stu);
		}
		br.close();
		return list;
		
	}
	/**
	 * 将list写入文件中
	 * @param filepath
	 * @param stu
	 * @throws IOException
	 */
	public static void writer(String filepath,ArrayList<Student> list) throws IOException{
		FileOutputStream fos=new FileOutputStream(filepath,true);
		OutputStreamWriter osw=new OutputStreamWriter(fos);
		PrintWriter pw=new PrintWriter(osw,true);
		for(int i=0;i<list.size();i++){
			Student stu=list.get(i);
			pw.println(stu.getId()+","+stu.getName()+","+stu.getAge()+","
			+stu.getAddress()+","+stu.getPhone()+","+stu.getOtherInfo());
		}
		pw.close();
		
	}
	/**
	 * 接受控制台输出的字符
	 * @param msg
	 * @return
	 */
	public static int inputInt(String msg){
		Scanner sc=new Scanner(System.in);
		System.out.println(msg);
		return sc.nextInt();
	}
	/**
	 * 接受控制台输出的int 类型数据
	 * @param msg
	 * @return
	 */
	public static String inputStr(String msg){
		Scanner sc=new Scanner(System.in);
		System.out.println(msg);
		return sc.next();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值