java基础_学生管理系统IO流版--2019/01/05

StudentManagerTest类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagerTest {
	public static void main(String[] args) throws IOException {
		//定义文件路径
		String fileName="student.txt";
		//为了创建student.txt文件
		File f=new File("student.txt");
		f.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.print("请输入你的选择:");
			Scanner scan = new Scanner(System.in);
			String choice=scan.nextLine();
			//switch语句实现
			switch(choice) {
			case "1":
				//查看所有学生
				findAllStudent(fileName);
				break;
			case "2":
				//添加学生
				addStudent(fileName);
				break;
			case "3":
				//删除学生
				deleteStudent(fileName);
				break;
			case "4":
				//修改学生
				updateStudent(fileName);
				break;
			case "5":
				//退出
			default:
				//输入有误
				System.out.println("谢谢你的使用");
				System.exit(0);
				break;
			}
		}
	}
	//查看所有学生
	public static void findAllStudent(String fileName) throws IOException {
		ArrayList<Student> stus=new ArrayList<Student>();
		readData(fileName,stus);
		//先判断集合是否有数据
		if(stus.size()==0) {
			System.out.println("目前没有学生数据可查询");
			return ;
		}
		System.out.println("学号\t姓名\t年龄\t地址");
		for (int i = 0; i < stus.size(); i++) {
			Student s=stus.get(i);
			System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
		}
	}
	//添加学生
	public static void addStudent(String fileName) throws IOException {
		ArrayList<Student> stus=new ArrayList<Student>();
		readData(fileName,stus);
		Scanner scan=new Scanner(System.in);
		//判断学号有没有被占用
		String id;
		while(true) {
			System.out.print("请输入学生学号:");
			id=scan.nextLine();
			boolean flag=false;
			for (int i = 0; i < stus.size(); i++) {
				Student s=stus.get(i);
				if(s.getId().equals(id)) {
					flag=true;
					break;
				}
			}
			if(flag) {
				System.out.println("你输入的学号已经被占用,请重新输入");
			}else {
				break;
			}
		}
		System.out.print("请输入学生姓名:");
		String name=scan.nextLine();
		System.out.print("请输入学生年龄:");
		String age=scan.nextLine();
		System.out.print("请输入学生地址:");
		String address=scan.nextLine();
		//创建一个学生对象
		Student stu=new Student(id,name,age,address);
		//添加到学生集合中
		stus.add(stu);
		System.out.println("添加成功");
		writeData(fileName,stus);
	}
	//删除学生(根据学号)
	public static void deleteStudent(String fileName) throws IOException {
		ArrayList<Student> stus=new ArrayList<Student>();
		readData(fileName,stus);
		System.out.println("请输入要删除的学生的学号:");
		Scanner scan=new Scanner(System.in);
		String id=scan.nextLine();
		int index=-1;
		for (int i = 0; i < stus.size(); i++) {
			Student s=stus.get(i);
			if(s.getId().equals(id)) {
				index=i;
				break;
			}
		}
		if(index!=-1) {
			stus.remove(index);
			System.out.println("删除成功");
		}else {
			System.out.println("未找到该学号的学生");
		}
		writeData(fileName,stus);
	}
	//修改学生
	public static void updateStudent(String fileName) throws IOException {
		ArrayList<Student> stus=new ArrayList<Student>();
		readData(fileName,stus);
		System.out.println("请输入要修改学生的学号:");
		Scanner scan=new Scanner(System.in);
		String id=scan.nextLine();
		
		int index=-1;
		for (int i = 0; i < stus.size(); i++) {
			Student s=stus.get(i);
			if(s.getId().equals(id)) {
				index=i;
				break;
			}
		}
		
		if(index==-1) {
			System.out.println("对不起,您要修改的学生的学号不存在");
		}else {
			Student s=stus.get(index);
			System.out.print("请输入学生新姓名:");
			String name=scan.nextLine();
			System.out.print("请输入学生新年龄:");
			String age=scan.nextLine();
			System.out.print("请输入学生新地址:");
			String address=scan.nextLine();
			s.setName(name);
			s.setAge(age);
			s.setAddress(address);
			System.out.println("修改学生成功");
		}
		writeData(fileName,stus);
	}
	//从文件中读数据
	public static void readData(String fileName,ArrayList<Student> array) throws IOException{
		BufferedReader br=new BufferedReader(new FileReader(fileName));
		String line;
		while((line=br.readLine())!=null) {
			String[] datas=line.split(",");
			Student s=new Student(datas[0],datas[1],datas[2],datas[3]);
			array.add(s);
		}
		br.close();
	}
	//把集合中的数据写入文件
	public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
		BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
		for (int i = 0; i < array.size(); i++) {
			Student s=array.get(i);
			StringBuilder sb=new StringBuilder();
			sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
			bw.write(sb.toString());
			bw.flush();
		}
		bw.close();
	}
}

Student类

public class Student {
	private String id;
	private String name;
	private String age;
	private String address;
	public Student() {
	}
	public Student(String id, String name, String age, String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String 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;
	}
	
}

使用前先在项目的根目录创建一个student.txt文件

 

 

Java学生管理系统中的IO流主要用于读取和写入学生信息到文件中。下面是一个简单的Java学生管理系统的示例代码,演示了如何使用IO流进行学生信息的读取和写入: ```java import java.io.*; import java.util.ArrayList; import java.util.List; // 学生类 class Student implements Serializable { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } // 学生管理系统类 class StudentManagementSystem { private List<Student> students; public StudentManagementSystem() { students = new ArrayList<>(); } // 添加学生信息 public void addStudent(Student student) { students.add(student); } // 保存学生信息到文件 public void saveToFile(String filename) { try { FileOutputStream fileOut = new FileOutputStream(filename); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(students); objectOut.close(); fileOut.close(); System.out.println("学生信息已保存到文件:" + filename); } catch (IOException e) { e.printStackTrace(); } } // 从文件中读取学生信息 public void loadFromFile(String filename) { try { FileInputStream fileIn = new FileInputStream(filename); ObjectInputStream objectIn = new ObjectInputStream(fileIn); students = (List<Student>) objectIn.readObject(); objectIn.close(); fileIn.close(); System.out.println("从文件中读取到的学生信息:"); for (Student student : students) { System.out.println("姓名:" + student.getName() + ",年龄:" + student.getAge()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } // 测试类 public class Main { public static void main(String[] args) { StudentManagementSystem system = new StudentManagementSystem(); // 添加学生信息 system.addStudent(new Student("张三", 18)); system.addStudent(new Student("李四", 20)); system.addStudent(new Student("王五", 22)); // 保存学生信息到文件 system.saveToFile("students.dat"); // 从文件中读取学生信息 system.loadFromFile("students.dat"); } } ``` 运行结果: ``` 学生信息已保存到文件:students.dat 从文件中读取到的学生信息: 姓名:张三,年龄:18 姓名:李四,年龄:20 姓名:王五,年龄:22 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值