欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年04月28日 17:14:58   作者:麋鹿的小羊駝  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)IO版本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

學(xué)生信息管理系統(tǒng)IO版本代碼實(shí)現(xiàn)(java),供大家參考,具體內(nèi)容如下

之前寫過的一個(gè)學(xué)生信息管理系統(tǒng)是用集合類來寫的,但是不能實(shí)現(xiàn)代碼在文檔中的存儲(chǔ)功能,每次運(yùn)行過后都得重新輸入數(shù)據(jù),無法做到保存的功能。

而用IO流進(jìn)行學(xué)生信息管理系統(tǒng)的編寫以后將數(shù)據(jù)存儲(chǔ)在文本文件中,以后每次訪問都可以訪問到之前已經(jīng)存到的數(shù)據(jù),類似于數(shù)據(jù)庫的一個(gè)存儲(chǔ)功能(這里并沒有用到Mysql數(shù)據(jù)庫,僅僅是用文本文檔來進(jìn)行數(shù)據(jù)的一系列存儲(chǔ))

以下是代碼的實(shí)現(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("----歡迎來到學(xué)生信息管理系統(tǒng)----");
  System.out.println("請(qǐng)輸入你想要進(jìn)行的操作");
  System.out.println("1:查看所有學(xué)生信息");
  System.out.println("2:添加學(xué)生信息");
  System.out.println("3:刪除學(xué)生信息");
  System.out.println("4:修改學(xué)生信息");
  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;
  }
  
 }
 }
 
 //從文件中讀數(shù)據(jù)到集合
  public static void readData(String fileName,ArrayList<Student> array) throws IOException {
   //創(chuàng)建輸入緩沖流對(duì)象
   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();
  }
 //把集合中的數(shù)據(jù)寫入文件
  public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
   //創(chuàng)建輸出緩沖流對(duì)象
   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();
  }
 //修改學(xué)生信息
 public static void updateStudent(String FileName) throws IOException {
  //創(chuàng)建集合對(duì)象
  ArrayList<Student> array = new ArrayList<Student>();
  //從文件中把數(shù)據(jù)讀取到集合中
  readData(FileName, array);
    
  Scanner scanner = new Scanner(System.in);
  while(true) {
  System.out.println("請(qǐng)輸入你要修改的學(xué)號(hào):");
  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("您輸入的學(xué)號(hào)有誤請(qǐng)重新輸入");
   continue;
  }else {
   System.out.println("請(qǐng)輸入新的姓名:");
   String name = scanner.nextLine();
   System.out.println("請(qǐng)輸入新的年齡:");
   String age = scanner.nextLine();
   System.out.println("請(qǐng)輸入新的地址");
   String address = scanner.nextLine();
   
   Student student = new Student();
   student.setId(id);
   student.setName(name);
   student.setAge(age);
   student.setAddress(address);
   
   array.set(index, student);
   //把集合中的數(shù)據(jù)重新寫回到文件
   writeData(FileName, array);
   break;
   }
  }
  System.out.println("修改學(xué)生成功");
  
 }
 
 //刪除學(xué)生信息
 public static void deleteStudent(String FileName) throws IOException {
  
  //創(chuàng)建集合對(duì)象
  ArrayList<Student> array = new ArrayList<Student>();
  //從文件中把數(shù)據(jù)讀取到集合中
  readData(FileName, array);
    
  Scanner scanner = new Scanner(System.in);
  while(true) {
  System.out.println("請(qǐng)輸入你要?jiǎng)h除的學(xué)號(hào)");
  
  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("您輸入的學(xué)號(hào)有誤 請(qǐng)重新輸入");
      continue;
     }else {
      array.remove(index);
      //把集合中的數(shù)據(jù)重新寫回到文件
   writeData(FileName, array);
      break;
     }
  }
  System.out.println("刪除學(xué)生信息成功!");
 }
 
 //添加學(xué)生信息
 public static void addStudent(String FileName) throws IOException {
  
  //創(chuàng)建集合對(duì)象
    ArrayList<Student> array = new ArrayList<Student>();
    //從文件中把數(shù)據(jù)讀取到集合中
    readData(FileName, array);
    
  Scanner scanner = new Scanner(System.in);
  String id;
  while(true) {
  System.out.println("請(qǐng)輸入你要添加的學(xué)號(hào):");
  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("你輸入的學(xué)號(hào)已被占用,請(qǐng)重新輸入");
    break;
   }else {
    flag++;
   }
   }
  if(flag==array.size()) {
   break;
  }
  }
  System.out.println("請(qǐng)輸入你要添加的姓名:");
  String name = scanner.nextLine();
  System.out.println("請(qǐng)輸入你要添加的年齡:");
  String age = scanner.nextLine();
     System.out.println("請(qǐng)輸入你要添加的地址:");
     String address = scanner.nextLine();
  Student student = new  Student();
  student.setId(id);
  student.setName(name);
  student.setAge(age);
  student.setAddress(address);
  
  array.add(student);
  //把集合中的數(shù)據(jù)重新寫回到文件
    writeData(FileName, array);
  
  System.out.println("添加信息成功"); 
 }
 
 //查看所有學(xué)生信息
 public static void findAllStudents(String FileName) throws IOException {
  
  //創(chuàng)建集合對(duì)象
    ArrayList<Student> array = new ArrayList<Student>();
    //從文件中把數(shù)據(jù)讀取到集合中
    readData(FileName, array);
    
  if(array.size()==0) {
   System.out.println("當(dāng)前沒有任何學(xué)生信息,請(qǐng)?zhí)砑雍笤偈褂?);
  }
  System.out.println("學(xué)號(hào)\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;
 }
 }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)現(xiàn)堆排序(大根堆)的示例代碼

    Java實(shí)現(xiàn)堆排序(大根堆)的示例代碼

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

    Java 線程池詳解

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

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

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

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

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

    淺談Java堆外內(nèi)存之突破JVM枷鎖

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

    Jasypt的StandardPBEByteEncryptor使用源碼解析

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

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

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

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

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

    java類成員中的訪問級(jí)別淺析

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

    Java通過apache poi生成excel實(shí)例代碼

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

最新評(píng)論