1. 程式人生 > >Java:學員管理系統:檔案IO流版

Java:學員管理系統:檔案IO流版

下面將寫出具體修改,最後將放出修改過的具體程式碼以供參考:

簡單版的資訊初始化不再使用,將初始化的內容寫入到一個檔案中,方法在使用學員集合時只需要呼叫一個將檔案內容讀取並返回一個學員集合的方法即可;

具體讀取檔案的方法:

//從檔案中讀取出Student集合並返回
    private static ArrayList<Student> readAll() {
        ArrayList<Student> stuList = new ArrayList<>();
        //學員的初始內容寫在專案下的 data.txt 檔案中
        //使用高效輸入流讀取檔案中的資訊
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) { String s = null; while ((s = br.readLine())!=null){ String[] strings = s.split(",");//檔案中的每個學員的具體資訊是以“,”號隔開的 Student student = new Student(); student.
setStuNo(Integer.parseInt(strings[0]));//學號 student.setName(strings[1]);//姓名 student.setSex(strings[2]);//性別 student.setAge(Integer.parseInt(strings[3]));//年齡 stuList.add(student);//將讀取到學員資訊新增到集合中 } return stuList;//返回該集合 }
catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null;//當檔案內容為空時返回null }

在新增、修改、刪除方法中,都對學員集合做出了修改,那就需要更新檔案中的學生資訊,所以就需要一個將修改過的學員集合寫入到檔案中的方法:

//將修改過的集合寫入到檔案中
    public static void writeAll(ArrayList<Student> stuList){
        try (BufferedWriter out = new BufferedWriter(new FileWriter("data.txt"))) {
            //使用覆蓋寫,將舊的檔案資訊以新的進行覆蓋
            for (Student s:stuList) {
                out.write(s.getStuNo()+","+s.getName()+","+s.getSex()+","+s.getAge());
                out.newLine();//相當於回車,一個學員資訊佔一行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

完整程式碼

Student類:

public class Student {
    private int stuNo;
    private String name;
    private String sex;
    private int age;

    public Student() {
    }

    public Student(int stuNo, String name, String sex, int age) {
        this.stuNo = stuNo;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

測試類:

package cn.khd.Test01;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("歡迎使用學生管理系統");
        while (true){
            System.out.println("1、新增 2、修改 3、刪除 4、按學號查詢 5、查詢全部 6、退出");
            System.out.println("請輸入要進行的操作:");
            int num = scanner.nextInt();
            switch (num){
                case 1:
                    addStuent(scanner);
                    break;
                case 2:
                    upData(scanner);
                    break;
                case 3:
                    delectStudent(scanner);
                    break;
                case 4:
                    findById(scanner);
                    break;
                case 5:
                    finAll();
                    break;
                case 6:
                    System.out.println("謝謝使用,已退出");
                    System.exit(0);
                    break;
                    default:
                        System.out.println("輸入有誤,請重新輸入");
                        break;
            }
        }
    }

    private static void upData(Scanner scanner) {
        ArrayList<Student> stuList = readAll();
        int n = 0;
        loop:
        while (true){
            System.out.println("請輸入要修改的學號:");
            n = scanner.nextInt();
            for (Student s:stuList) {
                if (s.getStuNo() == n){
                    System.out.println("你所要修改的資訊為:");
                    System.out.println(s);
                    System.out.println("請輸入你要修改的姓名(輸入0不修改):");
                    String name = scanner.next();
                    System.out.println("請輸入你要修改的性別(輸入0不修改):");
                    String sex = scanner.next();
                    System.out.println("請輸入你要修改的年齡(輸入0不修改):");
                    int age = scanner.nextInt();
                    if (!name.equals("0")){
                        s.setName(name);
                    }
                    if (!sex.equals("0")){
                        s.setSex(sex);
                    }
                    if (age != 0){
                        s.setAge(age);
                    }
                    writeAll(stuList);
                    System.out.println("已修改");
                    return;
                }

            }
            System.out.println("你輸入的學號不存在,請重新輸入");
            continue loop;
        }
    }

    private static void delectStudent(Scanner scanner) {
        ArrayList<Student> stuList = readAll();
        int n = 0;
        loop:
        while (true){
            System.out.println("請輸入要刪除的學號:");
            n = scanner.nextInt();
            for (Student s:stuList) {
                if (s.getStuNo() == n){
                    System.out.println("你所要刪除的資訊為:");
                    System.out.println(s);
                    System.out.println("確認是否刪除(y/n):");
                    String chack = scanner.next();
                    if (chack.equalsIgnoreCase("y")){
                        stuList.remove(s);
                        writeAll(stuList);
                        System.out.println("已刪除");
                    }

                    return;
                }
            }
            System.out.println("你輸入的學號不存在,請重新輸入");
            continue loop;
        }
    }

    private static void findById(Scanner scanner) {
        ArrayList<Student> stuList = readAll();
        int n = 0;
        loop:
        while (true){
            System.out.println("請輸入要查詢的學號:");
            n = scanner.nextInt();
            for (Student s:stuList) {
                if (s.getStuNo() == n){
                    System.out.println("你所查詢的資訊為:");
                    System.out.println(s);
                    return;
                }
            }
            System.out.println("你輸入的學號不存在,請重新輸入");
            continue loop;
        }
    }

    private static void addStuent(Scanner scanner) {
        ArrayList<Student> stuList = readAll();
        int n = 0;
        loop:
        while (true){
            System.out.println("請輸入要新增的學號:");
            n = scanner.nextInt();
            for (Student s:stuList) {
                if (s.getStuNo() == n){
                    System.out.println("你輸入的學號已存在,請重新輸入");
                    continue loop;
                }
            }
            System.out.println("請輸入要新增的姓名:");
            String name = scanner.next();
            System.out.println("請輸入要新增的性別:");
            String sex = scanner.next();
            System.out.println("請輸入要新增的年齡:");
            int age = scanner.nextInt();
            Student student = new Student(n,name,sex,age);
            stuList.add(student);
            writeAll(stuList);
            break ;
        }
    }

    private static void finAll() {
        ArrayList<Student> stuList = readAll();
        System.out.println("-----------------------------");
        System.out.println("學號\t\t姓名\t\t性別\t\t年齡");
        System.out.println(".............................");
        if (stuList == null || stuList.size() == 0){
            //stuList必須寫在前面,短路或,如果stuList.size()==0寫前面可能出現空指標異常
            System.out.println("無資訊");
        }else {
            for (Student s:stuList) {
                System.out.println(s.getStuNo()+"\t\t"+s.getName()+"\t\t"+s.getSex()+"\t\t"+s.getAge());
            }
        }
        System.out.println("-----------------------------");
    }
    //從檔案中讀取出Student集合並返回
    private static ArrayList<Student> readAll() {
        ArrayList<Student> stuList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
            String s = null;
            while ((s = br.readLine())!=null){
                String[] strings = s.split(",");
                Student student = new Student();
                student.setStuNo(Integer.parseInt(strings[0]));
                student.setName(strings[1]);
                student.setSex(strings[2]);
                student.setAge(Integer.parseInt(strings[3]));
                stuList.add(student);
            }
            return stuList;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //將修改過的集合寫入到檔案中
    public static void writeAll(ArrayList<Student> stuList){
        try (BufferedWriter out = new BufferedWriter(new FileWriter("data.txt"))) {
            //使用覆蓋寫,將舊的檔案資訊以新的進行覆蓋
            for (Student s:stuList) {
                out.write(s.getStuNo()+","+s.getName()+","+s.getSex()+","+s.getAge());
                out.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}