亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)學生信息管理系統(tǒng)IO版本

 更新時間:2021年04月28日 17:14:58   作者:麋鹿的小羊駝  
這篇文章主要為大家詳細介紹了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)堆排序(大根堆)的示例代碼

    這篇文章主要介紹了Java實現(xiàn)堆排序(大根堆)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • Java 線程池詳解

    Java 線程池詳解

    本文給大家總結了java中的線程池的相關問題,非常的詳細也很實用,有需要的小伙伴可以參考下。
    2016-03-03
  • MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題

    MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題

    我們經常會遇到表中的字段名和表對應實體類的屬性名稱不一定都是完全相同的情況,如何解決呢?下面腳本之家小編給大家介紹MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題,一起學習吧
    2016-05-05
  • JAVA加密算法數字簽名實現(xiàn)原理詳解

    JAVA加密算法數字簽名實現(xiàn)原理詳解

    這篇文章主要介紹了JAVA加密算法數字簽名實現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • 淺談Java堆外內存之突破JVM枷鎖

    淺談Java堆外內存之突破JVM枷鎖

    這篇文章主要介紹了淺談Java堆外內存之突破JVM枷鎖,涉及jvm內存分配,jvm垃圾回收,堆外內存的垃圾回收等相關內容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Jasypt的StandardPBEByteEncryptor使用源碼解析

    Jasypt的StandardPBEByteEncryptor使用源碼解析

    這篇文章主要介紹了Jasypt的StandardPBEByteEncryptor使用源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能

    MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能

    這篇文章主要介紹了MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • java實現(xiàn)短地址服務的方法(附代碼)

    java實現(xiàn)短地址服務的方法(附代碼)

    大多數情況下URL太長,字符多,不便于發(fā)布復制和存儲,本文就介紹了通過java實現(xiàn)短地址服務,減少了許多使用太長URL帶來的不便,需要的朋友可以參考下
    2015-07-07
  • java類成員中的訪問級別淺析

    java類成員中的訪問級別淺析

    在本篇文章里小編給大家整理的是一篇關于java類成員中的訪問級別淺析內容,有興趣的朋友們跟著學習下。
    2021-01-01
  • Java通過apache poi生成excel實例代碼

    Java通過apache poi生成excel實例代碼

    本篇文章主要介紹了Java通過apache poi生成excel實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論