Java實現(xiàn)學生信息管理系統(tǒng)IO版本
學生信息管理系統(tǒng)IO版本代碼實現(xiàn)(java),供大家參考,具體內容如下
之前寫過的一個學生信息管理系統(tǒng)是用集合類來寫的,但是不能實現(xiàn)代碼在文檔中的存儲功能,每次運行過后都得重新輸入數據,無法做到保存的功能。
而用IO流進行學生信息管理系統(tǒng)的編寫以后將數據存儲在文本文件中,以后每次訪問都可以訪問到之前已經存到的數據,類似于數據庫的一個存儲功能(這里并沒有用到Mysql數據庫,僅僅是用文本文檔來進行數據的一系列存儲)
以下是代碼的實現(xiàn)過程:
主類
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("----歡迎來到學生信息管理系統(tǒng)----"); 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("正在退出系統(tǒng),歡迎下次繼續(xù)使用"); System.exit(0);//JVM退出 break; } } } //從文件中讀數據到集合 public static void readData(String fileName,ArrayList<Student> array) throws IOException { //創(chuàng)建輸入緩沖流對象 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 { //創(chuàng)建輸出緩沖流對象 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 { //創(chuàng)建集合對象 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 { //創(chuàng)建集合對象 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 { //創(chuàng)建集合對象 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 { //創(chuàng)建集合對象 ArrayList<Student> array = new ArrayList<Student>(); //從文件中把數據讀取到集合中 readData(FileName, array); if(array.size()==0) { System.out.println("當前沒有任何學生信息,請?zhí)砑雍笤偈褂?); } 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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Java實現(xiàn)哈希表的基本功能
- Java實現(xiàn)FTP文件上傳
- 教你用Java實現(xiàn)RSA非對稱加密算法
- java實現(xiàn)樹形菜單對象
- Java實現(xiàn)多線程中的靜態(tài)代理模式
- 我用java實現(xiàn)了王者榮耀的皮膚和英雄技能
- java實現(xiàn)Linux(centos) 中docker容器下命令交互的代碼(配置向導)
- java實現(xiàn)表單必填參數驗證的方法
- 用Java實現(xiàn)24點游戲
- Java實現(xiàn)NIO聊天室的示例代碼(群聊+私聊)
- Java實現(xiàn)簡單的掃雷圖
- 圖文詳解JAVA實現(xiàn)快速排序
- 教你怎么使用Java實現(xiàn)WebSocket
- Java實現(xiàn)雪花算法的原理
- 教你怎么用Java實現(xiàn)給圖片打上水印
- 教你怎么用java實現(xiàn)客戶端與服務器一問一答
- Java實現(xiàn)學生成績管理系統(tǒng)
- Java數據結構之實現(xiàn)跳表
相關文章
MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題
我們經常會遇到表中的字段名和表對應實體類的屬性名稱不一定都是完全相同的情況,如何解決呢?下面腳本之家小編給大家介紹MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題,一起學習吧2016-05-05Jasypt的StandardPBEByteEncryptor使用源碼解析
這篇文章主要介紹了Jasypt的StandardPBEByteEncryptor使用源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能
這篇文章主要介紹了MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06