## 利用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 list=read(filepath);
		for(int i=0;i li=new ArrayList();
		li.add(stu);
		ArrayList list=read(filepath);
		if(list.size()==0){
			writer(filepath,li);
			System.out.println("添加成功");
			return;
		}else{
			for(int i=0;i list=read(filepath);
		for(int i=0;i list=read(filepath);
		for(int i=0;i list=read(filepath);
		int index=-1;
		for(int i=0;i list=read(filepath);
		int index=-1;
		for(int i=0;i read(String filepath) throws IOException{
		FileInputStream fis=new FileInputStream(filepath);
		InputStreamReader ir=new InputStreamReader(fis);
		BufferedReader br=new BufferedReader(ir);
		ArrayList list=new ArrayList();
		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 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

你可能感兴趣的:(## 利用java的IO流知识编写简单的学生管理系统)