国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java實(shí)現學(xué)生信息管理系統IO版本

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:麋鹿的小羊駝 欄目: 編程語(yǔ)言 歡迎投稿:712375056

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

之前寫(xiě)過(guò)的是用集合類(lèi)來(lái)寫(xiě)的,但是不能實(shí)現代碼在文檔中的存儲功能,每次運行過(guò)后都得重新輸入數據,無(wú)法做到保存的功能。

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

以下是代碼的實(shí)現過(guò)程:

主類(lèi)

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("----歡迎來(lái)到學(xué)生信息管理系統----");
  System.out.println("請輸入你想要進(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("正在退出系統,歡迎下次繼續使用");
   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();
  }
 //把集合中的數據寫(xiě)入文件
  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();
  }
 //修改學(xué)生信息
 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("請輸入你要修改的學(xué)號:");
  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é)號有誤請重新輸入");
   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);
   //把集合中的數據重新寫(xiě)回到文件
   writeData(FileName, array);
   break;
   }
  }
  System.out.println("修改學(xué)生成功");
  
 }
 
 //刪除學(xué)生信息
 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("請輸入你要刪除的學(xué)號");
  
  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é)號有誤 請重新輸入");
      continue;
     }else {
      array.remove(index);
      //把集合中的數據重新寫(xiě)回到文件
   writeData(FileName, array);
      break;
     }
  }
  System.out.println("刪除學(xué)生信息成功!");
 }
 
 //添加學(xué)生信息
 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("請輸入你要添加的學(xué)號:");
  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é)號已被占用,請重新輸入");
    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);
  //把集合中的數據重新寫(xiě)回到文件
    writeData(FileName, array);
  
  System.out.println("添加信息成功"); 
 }
 
 //查看所有學(xué)生信息
 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("當前沒(méi)有任何學(xué)生信息,請添加后再使用");
  }
  System.out.println("學(xué)號\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類(lèi)

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;
 }
 }

以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

ja
亚洲国产精品成人精品无码区在线| 手机看片久久国产免费| 亚洲天堂2017无码| 加勒比一区二区无码视频在线| 中文天堂网在线最新版| 草莓AV福利网站导航|