Java实现学生信息管理系统IO版本

网友投稿 249 2023-01-19

Java实现学生信息管理系统IO版本

学生信息管理系统IO版本代码实现(java),供大家参考,具体内容如下

之前写过的一个学生信息管理系统是用集合类来写的,但是不能实现代码在文档中的存储功能,每次运行过后都得重新输入数据,无法做到保存的功能。

而用IO流进行学生信息管理系统的编写以后将数据存储在文本文件中,以后每次访问都可以访问到之前已经存到的数据,类似于数据库的一个存储功能(这里并没有用到mysql数据库,仅仅是用文本文档来进行数据的一系列存储)

以下是代码的实现过程:

主类

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 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 array) throws IOException {

//创建输出缓冲流对象

BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

for(int x=0; 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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

readData(FileName, array);

if(array.size()==0) {

System.out.println("当前没有任何学生信息,请添加后再使用");

}

System.out.println("学号\t姓名\t年龄\t居住地");

for(int x=0; x

Student s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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 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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

readData(FileName, array);

if(array.size()==0) {

System.out.println("当前没有任何学生信息,请添加后再使用");

}

System.out.println("学号\t姓名\t年龄\t居住地");

for(int x=0; x

Student s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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 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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

readData(FileName, array);

if(array.size()==0) {

System.out.println("当前没有任何学生信息,请添加后再使用");

}

System.out.println("学号\t姓名\t年龄\t居住地");

for(int x=0; x

Student s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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 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 array = new ArrayList();

//从文件中把数据读取到集合中

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

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 array = new ArrayList();

//从文件中把数据读取到集合中

readData(FileName, array);

if(array.size()==0) {

System.out.println("当前没有任何学生信息,请添加后再使用");

}

System.out.println("学号\t姓名\t年龄\t居住地");

for(int x=0; x

Student s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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 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 array = new ArrayList();

//从文件中把数据读取到集合中

readData(FileName, array);

if(array.size()==0) {

System.out.println("当前没有任何学生信息,请添加后再使用");

}

System.out.println("学号\t姓名\t年龄\t居住地");

for(int x=0; x

Student s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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 s = array.get(x);

System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

}

}

}

Student类

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) {fYPSwh

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;

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot实现物品收藏功能
下一篇:Java服务器主机信息监控工具类的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~