A student information management system is a collection of previously written class to write, but can not implement the code in the document storage function, after each run had to re-enter data, can not be saved function.
And be written until after the student information management system with IO stream data is stored in a text file, each subsequent visit can access to already existing data, similar to a stored database functions (here did not use Mysql database merely for data storage of a series of text documents)
the following procedure is implemented in the code:
the main class
package zjh;
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 StudentManageTest {
public static void main(String[] args) throws IOException {
String FileName = "students.txt";
while(true) {
System.out.println("----欢迎来到学生信息管理系统----");
System.out.println("请输入你想要进行的操作");
System.out.println("1:查看所有学生信息");
System.out.println("2:添加学生信息");
System.out.println("3:删除学生信息");
System.out.println("4:修改学生信息");
System.out.println("5:退出");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
switch (choice) {
case "1":
findAllStudents(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);//JVM退出
break;
}
}
}
//从文件中读数据到集合
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();
s.setId(datas[0]);
s.setName(datas[1]);
s.setAge(datas[2]);
s.setAddress(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 x=0; x<array.size(); x++) {
Student s = array.get(x);
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.newLine();
bw.flush();
}
bw.close();
}
//修改学生信息
public static void updateStudent(String FileName) throws IOException {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//从文件中把数据读取到集合中
readData(FileName, array);
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入你要修改的学号:");
String id = scanner.nextLine();
int index = -1;
for(int x=0; x<array.size(); x++) {
Student student = array.get(x);
if(student.getId().equals(id)) {
index = x;
break;
}
}
if(index ==-1) {
System.out.println("您输入的学号有误请重新输入");
continue;
}else {
System.out.println("请输入新的姓名:");
String name = scanner.nextLine();
System.out.println("请输入新的年龄:");
String age = scanner.nextLine();
System.out.println("请输入新的地址");
String address = scanner.nextLine();
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
array.set(index, student);
//把集合中的数据重新写回到文件
writeData(FileName, array);
break;
}
}
System.out.println("修改学生成功");
}
//删除学生信息
public static void deleteStudent(String FileName) throws IOException {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//从文件中把数据读取到集合中
readData(FileName, array);
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入你要删除的学号");
String id = scanner.nextLine();
int index = -1;
for(int x=0; x<array.size(); x++) {
Student student = array.get(x);
if (student.getId().equals(id)) {
index = x;
break;
}
}
if(index == -1) {
System.out.println("您输入的学号有误 请重新输入");
continue;
}else {
array.remove(index);
//把集合中的数据重新写回到文件
writeData(FileName, array);
break;
}
}
System.out.println("删除学生信息成功!");
}
//添加学生信息
public static void addStudent(String FileName) throws IOException {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//从文件中把数据读取到集合中
readData(FileName, array);
Scanner scanner = new Scanner(System.in);
String id;
while(true) {
System.out.println("请输入你要添加的学号:");
int flag =0;
id = scanner.nextLine();
for(int x=0; x<array.size(); x++) {
Student student =array.get(x);
if(student.getId().equals(id)) {
System.out.println("你输入的学号已被占用,请重新输入");
break;
}else {
flag++;
}
}
if(flag==array.size()) {
break;
}
}
System.out.println("请输入你要添加的姓名:");
String name = scanner.nextLine();
System.out.println("请输入你要添加的年龄:");
String age = scanner.nextLine();
System.out.println("请输入你要添加的地址:");
String address = scanner.nextLine();
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
array.add(student);
//把集合中的数据重新写回到文件
writeData(FileName, array);
System.out.println("添加信息成功");
}
//查看所有学生信息
public static void findAllStudents(String FileName) throws IOException {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//从文件中把数据读取到集合中
readData(FileName, array);
if(array.size()==0) {
System.out.println("当前没有任何学生信息,请添加后再使用");
}
System.out.println("学号\t姓名\t年龄\t居住地");
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
Student category
package zjh;
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;
}
}
the above