定义学生类
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;
}
}
主函数
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 StudentManageText {
public static void main(String[] args) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
String fileName = "student.txt";
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.退出");
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的选择:");
int choice = sc.nextInt();
switch(choice){
case 1:
findAllStudent(fileName);
break;
case 2:
addStudent(fileName);
break;
case 3:
removeStudent(fileName);
break;
case 4:
updateStudent(fileName);
break;
case 5:
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("您输入的操作有误,请重新输入!");
break;
}
}
}
public static void findAllStudent(String fileName) throws IOException{
Student student = new Student();
ArrayList<Student> array = new ArrayList<Student>();
readFile(fileName,array);
if(array.size()==0){
System.out.println("非常抱歉,当前没有学生信息可供查看!");
}else{
System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"家庭住址");
for(int i=0;i<array.size();i++){
student = array.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());
}
}
}
public static void addStudent(String fileName) throws IOException{
Scanner sc = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
readFile(fileName,array);
String id;
while(true){
System.out.println("请输入学生学号:");
id = sc.nextLine();
boolean flag = false;
for(int i=0;i<array.size();i++){
Student student = array.get(i);
if(student.getId().equals(id)){
flag = true;
break;
}
}
if(flag){
System.out.println("您输入的学号已存在,请重新输入!");
}else{
break;
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
System.out.println("请输入学生家庭住址:");
String address = sc.nextLine();
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
array.add(student);
writeFile(fileName,array);
System.out.println("添加学生成功!");
}
public static void removeStudent(String fileName) throws IOException{
Scanner sc = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
readFile(fileName,array);
System.out.println("请输入要删除的学生学号:");
String id = sc.nextLine();
int index = -1;
for(int i=0;i<array.size();i++){
Student student = array.get(i);
if(student.getId().equals(id)){
index = i;
break;
}
}
if(index==-1){
System.out.println("非常抱歉,您要删除的学生不存在,请重新输入!");
}else{
array.remove(index);
writeFile(fileName,array);
System.out.println("删除学生成功!");
}
}
public static void updateStudent(String fileName) throws IOException{
Scanner sc = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
readFile(fileName,array);
System.out.println("请输入需要修改学生的学号:");
String id = sc.nextLine();
int index = -1;
for(int i=0;i<array.size();i++){
Student student = array.get(i);
if(student.getId().equals(id)){
index = i;
break;
}
}
if(index==-1){
System.out.println("非常抱歉,您要修改的学生信息不存在,请重新输入!");
}else{
Student student = new Student();
System.out.println("请输入新的学生姓名:");
String name = sc.nextLine();
System.out.println("请输入新的学生年龄:");
String age = sc.nextLine();
System.out.println("请输入新的学生家庭住址:");
String address = sc.nextLine();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
array.set(index,student);
writeFile(fileName,array);
System.out.println("恭喜修改成功!");
}
}
public static void readFile(String fileName,ArrayList<Student> array) throws IOException{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line;
while((line=br.readLine()) != null){
String[] datas = line.split(",");
Student student = new Student();
student.setId(datas[0]);
student.setName(datas[1]);
student.setAge(datas[2]);
student.setAddress(datas[3]);
array.add(student);
}
br.close();
}
public static void writeFile(String fileName,ArrayList<Student> array) throws IOException{
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
for(int i=0;i<array.size();i++){
Student student = array.get(i);
StringBuilder sb = new StringBuilder();
sb.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}