学生情報管理システムIOバージョンコード実装(java)
前に書いた学生情報管理システムは集合クラスで書かれていますが、ドキュメントにコードを格納する機能は実現できません.実行するたびにデータを再入力しなければなりません.保存できない機能です.一方、IOストリームで学生情報管理システムの作成を行った後、データをテキストファイルに格納し、その後、アクセスするたびに以前に格納されていたデータにアクセスすることができ、データベースの記憶機能(ここではMysqlデータベースは使用されず、文書ドキュメントでデータを格納する一連の記憶のみ)と同様に、コードの実現過程である.
Studentクラス
以上
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クラス
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;
}
}
以上