day25【综合案例-学生管理系统豪华升级版 (网络编程,io序列化流,多线程实现C\S实现)】

任务目标

  • 需求
    -使用网络编程,IO流,序列化流,多线程实现C\S结构的学生信息管理系统
    在这里插入图片描述
    思路分析:
    1.对象需要用到序列化,创建的学生类实现Serializable接口
    2.保存在文件,第一次用文件有没有,没有需创建,同时如果防止没有数据的时候读取报错,需要创建一个空集合,序列化到文件中.
    3.服务器和客户端约定好一种格式 比如添加[1]数据 表示添加 传输的格式[1]张三,18,北京
    4.id需要自增,需用到静态代码块,读取文件,拿到list的长度来编号
    5.客户端发送请求送,服务器需先从文件中序列化集合出来,操作完后重写上去(数据覆盖)
    6.修改比较特殊,先判断id是否存在,存在的情况需再次链接数据库发送格式数据,进行修改

服务器端

1.服务器的菜单

public class Server {
    public static void main(String[] args) {
        try {
            // 创建ServerSocket对象
            ServerSocket ss = new ServerSocket(8888);
            // 由于客户端访问服务器一个功能就连接,不访问就断开,所以要循环接收客户端请求
            while (true){
                // 接收客户端请求
                Socket socket = ss.accept();
                // 获取客户端传过来的数据,得到编号,做出相应的处理
                InputStream is = socket.getInputStream();
                byte[] bys = new byte[1024];
                int len = is.read(bys);
                String message = new String(bys,0,len);
                // 增删查改的编号都是在索引为1的位置上,所以根据这个规律得到编号
                char op = message.charAt(1);
                // 根据编号作出相应的处理
                switch (op){
                    case '1':
                        // 添加
                        break;
                    case '2':
                        // 查询
                        break;
                    case '3':
                        // 删除
                        break;
                    case '4':
                        // 修改
                        break;
                    case '5':
                        // 查询一条
                        break;
                    default:
                        // 关闭连接
                        System.out.println("格式错误!");
                        socket.close();
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.服务器的添加功能

 // 添加
    private static void addStudent(Socket socket,String message) {
        try {
            // 创建反序列化流
            FileInputStream fis = new FileInputStream("day25\\stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读取文件中的集合
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            // 解析客户端传入的数据
            String[] arr = message.substring(3).split(",");
            String name = arr[0];
            int age = Integer.parseInt(arr[1]);
            String address = arr[2];

            // 创建Student对象,并给属性赋值
            Student stu = new Student(++Utils.id,name,age,address);
            // 把学生对象添加到集合中
            list.add(stu);

            // 创建序列化流
            FileOutputStream fos = new FileOutputStream("day25\\stu.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            // 把集合重新写回文件中
            oos.writeObject(list);

            // 写回数据给客户端
            OutputStream os = socket.getOutputStream();
            os.write("添加成功".getBytes());

            // 关闭流,关闭socket
            fos.close();
            fis.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.服务器第一次启动创建文件,并写入一个空集合对象到文件中

// 在服务器的main方法下第一行
public class Server {

    public static void main(String[] args) {
        try {
            // 开始启动服务器,文件不存在,得创建文件,但只有第一次启动的时候才可以创建,后期就不能创建
            File file = new File("day25\\stu.txt");
            if (!file.exists()){
                file.createNewFile();
            }
            // 空文件,创建反序列化流
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 释放资源
            ois.close();
        } catch (Exception e) {
            // 产生异常,说明文件中没有内容,那么就写入一个空集合对象到文件中
            try {
                // 创建序列化流
                FileOutputStream fos = new FileOutputStream("day25\\stu.txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                // 把空集合对象添加到文件中
                ArrayList<Student> list = new ArrayList<>();
                oos.writeObject(list);
                // 释放资源
                oos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    // 创建服务器对象,等待客户端连接服务器.
    // ...省略.....
    }
}

4.服务器的查询功能

// 查询
    private static void selectStudent(Socket socket, String message) {
        try {
            // 创建反序列化流
            FileInputStream fis = new FileInputStream("day25\\stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读取文件中的集合
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();

            // 通过socket对象获取输出流
            OutputStream os = socket.getOutputStream();
            // 转换为序列化流
            ObjectOutputStream oos = new ObjectOutputStream(os);
            // 写出集合对象
            oos.writeObject(list);

            // 释放资源
            ois.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5.服务器的删除功能

 // 删除
    private static void deleteStudent(Socket socket, String message) {
        // 按照格式获取客户端要删除的学员编号
        String msg = message.substring(3);
        int id = Integer.parseInt(msg);

        try {
            // 创建反序列流
            FileInputStream fis = new FileInputStream("day25\\stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读取文件中的集合对象
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();

            // 判断集合中是否有该id的学员
            // 定义一个变量,标记是否删除成功
            boolean flag = false;// 默认删除失败
            // 循环遍历集合查找,如果有直接删除
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                if (stu.getId() == id){
                    // 删除
                    list.remove(i);
                    // 修改标记
                    flag = true;
                    // 更新文件中的数据
                    // 创建序列化流
                    FileOutputStream fos = new FileOutputStream("day25\\stu.txt");
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    // 把集合重新写回文件中
                    oos.writeObject(list);
                    break;// 结束循环
                }
            }

            // 通过socket对象获取输出流
            OutputStream os = socket.getOutputStream();
            if (flag == true){
                os.write("删除成功".getBytes());
            }else {
                os.write("删除失败".getBytes());
            }

            // 关闭连接
            ois.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

6.服务器查询一条数据

// 查询一条数据
    private static void selectById(Socket socket, String message) {
        // 按照格式获取客户端要删除的学员编号
        String msg = message.substring(3);
        int id = Integer.parseInt(msg);

        try {
            // 创建反序列流
            FileInputStream fis = new FileInputStream("day25\\stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读取文件中的集合对象
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();

            // 定义以变量,用来标记该学生是否存在
            boolean flag = false;// 默认不存在
            // 循环遍历集合查找,如果有就修改标记
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                if (stu.getId() == id) {
                    flag = true;
                    break;
                }
            }

            // 根据标记,返回对于的内容
            // 通过socket对象获取输出流
            OutputStream os = socket.getOutputStream();
            if (flag == false){
                os.write(0);
            }else {
                os.write(1);
            }

            // 关闭连接,释放资源
            ois.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

7.服务器修改功能

 // 修改
    private static void updateStudent(Socket socket, String message) {
        // 按照格式获取客户端要删除的学员编号
        String msg = message.substring(3);
        String[] arr = msg.split(",");
        int id = Integer.parseInt(arr[0]);
        String name = arr[1];
        int age = Integer.parseInt(arr[2]);
        String address = arr[3];

        try {
            // 创建反序列流
            FileInputStream fis = new FileInputStream("day25\\stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读取文件中的集合对象
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            // 定义一个变量,用来标记是否修改成功
            boolean flag = false;// 默认修改失败
            // 循环遍历集合中的元素
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                if (stu.getId() == id){
                    if (!"0".equals(name)){
                        stu.setName(name);
                    }

                    if (!"0".equals(arr[2])){
                        stu.setAge(age);
                    }

                    if (!"0".equals(address)){
                        stu.setName(address);
                    }
                    flag = true;
                    break;
                }
            }

            System.out.println(list);
            // 把修改后的集合写回文件中:如果有修改,写的就是修改后的,如果没有修改,写的还是原来的
            FileOutputStream fos = new FileOutputStream("day25\\stu.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
            oos.close();

            // 把信息写回给客户端
            // 通过socket对象获取输出流
            OutputStream os = socket.getOutputStream();
            if (flag == true){
                os.write("修改成功".getBytes());
            }else{
                os.write("修改失败".getBytes());
            }

            // 关闭连接
            socket.close();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

客户端

1.客户端的菜单

public class Client {
    public static void main(String[] args) {
        // 创建Scanner对象
        Scanner sc = new Scanner(System.in);
        // 一级菜单循环显示
        while (true){
            System.out.println("******************************************************");
            System.out.println("1.添加学员\t2.查询学员\t3.删除学员\t4.修改学员\t5.退出");
            System.out.println("******************************************************");
            System.out.println("请输入功能序号:");
            int op = sc.nextInt();
            // 根据用户的选择执行对应的功能
            switch (op){
                case 1:
                    // 添加
                    break;
                case 2:
                    // 查询
                    break;
                case 3:
                    // 删除
                    break;
                case 4:
                    // 修改
                    break;
                case 5:
                    // 退出
                    System.out.println("谢谢使用,欢迎下次再来");
                    System.exit(0);
            }
        }
    }
}

2.客户端的添加功能

   // 添加
    private static void addStudent() {
        // 输入要添加的学生信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学员姓名:");
        String name = sc.next();
        System.out.println("请输入学员年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学员地址:");
        String address = sc.next();
        
        // 把数据按照一定格式拼接,服务器方便解析
        String msg = "[1]"+name+","+age+","+address;

        // 连接服务器
        try {
            // 创建Socket对象,连接服务器
            Socket socket = new Socket("127.0.0.1",8888);
            
            // 通过socket获取输出流
            OutputStream os = socket.getOutputStream();
            // 写出数据到服务器
            os.write(msg.getBytes());
            
            // 通过socket获取输入流
            InputStream is = socket.getInputStream();
            // 读取服务器回写的数据
            byte[] bys = new byte[1024];
            int len = is.read(bys);
            System.out.println(new String(bys,0,len));
            
            // 操作完了,关闭连接
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.客户端的查询功能

// 查询
    private static void selectStudent() {
        // 按照格式封装要传给服务器的数据
        String message = "[2]";

        try {
            // 创建Socket对象,连接服务器
            Socket socket = new Socket("127.0.0.1",8888);
            // 通过socket获得输出流
            OutputStream os = socket.getOutputStream();
            // 写出数据
            os.write(message.getBytes());

            // 通过socket获得输入流
            InputStream is = socket.getInputStream();
            // 转换为反序列流,读取服务器写回的集合对象
            ObjectInputStream ois = new ObjectInputStream(is);
            // 读取集合对象
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();

            if (list.size() == 0){
                System.out.println("没有数据");
            }else{
                for (Student stu : list) {
                    System.out.println(stu);
                }
            }

            // 释放资源
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

4.客户端的删除功能

 // 删除
    private static void deleteStudent() {
        // 输入要删除学生的id
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学员id:");
        int id = sc.nextInt();

        // 按照格式封装要传给服务器的数据
        String message = "[3]"+id;

        try {
            // 创建Socket对象,连接服务器
            Socket socket = new Socket("127.0.0.1",8888);
            // 通过socket获得输出流
            OutputStream os = socket.getOutputStream();
            // 写出数据
            os.write(message.getBytes());

            // 通过socket获得输入流
            InputStream is = socket.getInputStream();
            // 读取服务器回写的数据
            byte[] bys = new byte[1024];
            int len = is.read(bys);
            System.out.println(new String(bys,0,len));

            // 关闭连接
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5.客户端的修改功能

// 修改
    private static void updateStudent() {
        // 输入要修改学生的id
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学员id:");
        int id = sc.nextInt();

        // 按照格式封装要传给服务器的数据
        String message = "[5]"+id;

        // 连接服务器,把id发送给服务器
        try {
            // 创建Socket对象,连接服务器,
            Socket socket = new Socket("127.0.0.1",8888);
            // 通过socket获得输出流
            OutputStream os = socket.getOutputStream();
            // 写出数据
            os.write(message.getBytes());

            // 获取服务器返回结果,如果没有就结束修改,如果有,就重新连接服务器,写出要修改的数据
            InputStream is = socket.getInputStream();
            // 假设服务器返回0,表示没查询到,返回1表示查询到
            int len = is.read();
            if (len == 0){
                // 如果没有就结束修改
                System.out.println("没有改id,修改失败");
                socket.close();
                return;
            }else{
                // 断开连接
                socket.close();
                // 如果有,就重新连接服务器,写出要修改的数据
                try {
                    // 输入要修改的信息
                    System.out.println("请输入需要修改的姓名,0表示不修改:");
                    String name = sc.next();
                    System.out.println("请输入需要修改的年龄,0表示不修改:");
                    int age = sc.nextInt();
                    System.out.println("请输入需要修改的地址,0表示不修改:");
                    String address = sc.next();
                    // 按照格式封装要传给服务器的数据
                    String msg = "[4]"+id+","+name+","+age+","+address;

                    // 创建Socket对象,连接服务器,
                    Socket socket2 = new Socket("127.0.0.1",8888);
                    // 通过socket获得输出流
                    OutputStream os2 = socket2.getOutputStream();
                    // 写出数据
                    os2.write(msg.getBytes());

                    // 通过socket获得输入流
                    InputStream is2 = socket2.getInputStream();
                    // 读取服务器回写的数据
                    byte[] bys = new byte[1024];
                    int len2 = is2.read(bys);
                    System.out.println(new String(bys,0,len2));

                    // 关闭连接
                    socket2.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

6.服务器多线程

  • 如果有多个客户端连接服务器,那么服务器接收请求后,应该开辟多条线程来执行不同客户端的任务
 // 由于客户端访问服务器一个功能就连接,不访问就断开,所以要循环接收客户端请求
while (true){
    // 接收客户端请求
    Socket socket = ss.accept();
    // 接收连接后,开辟线程执行任务
    new Thread(()->{
        // 任务代码
        // ....
    }).start();
}

工具类

1.学生类

public class Student implements Serializable {// 需要实现序列化接口
    private int id;// 编号
    private String name;// 姓名
    private int age;// 年龄
    private String address;// 地址
    
    // 构造方法
    public Student() {
    }

    public Student(int id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    // setter\getter
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    // 可能需要打印
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}

最后的实现代码

目录

在这里插入图片描述

Student

public class Student implements Serializable {
    private int id;
    private String name;
    private int age;
    private String adress;

    public Student(int id, String name, int age, String adress) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.adress = adress;
    }

    public Student() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

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

Client

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("**********************************************");
            System.out.println("1.添加学员\t2.查询学员\t3.删除学员\t4.修改学员\t5.退出系统");
            System.out.println("**********************************************");
            int op = sc.nextInt();
            switch (op) {
                case 1:addStudent(sc);//添加
                    break;
                case 2:findStudent();//查询
                    break;
                case 3:deleteStudent(sc);//删除
                    break;
                case 4:updateStudent(sc);//修改
                    break;
                case 5:
                    System.out.println("谢谢使用");
                    System.exit(0);
            }
        }
    }

    private static void updateStudent(Scanner sc) {
        try {
            Socket socket = new Socket("localhost", 8888);
            OutputStream os = socket.getOutputStream();
            System.out.println("请输入要修改的学生id");
            int id = sc.nextInt();
            os.write(("[5]" + id).getBytes());

            InputStream is = socket.getInputStream();
            int read = is.read();
            if (read==0){
                System.out.println("没有查询到要修改的id");
                socket.close();
                return;
            }else {
                socket.close();
                try {

                    System.out.println("请输入要修改的学生姓名,如输入0视为保存原值");
                    String name = sc.next();
                    System.out.println("请输入要修改的学生年龄,如输入0视为保存原值");
                    int age = sc.nextInt();
                    System.out.println("请输入要修改的学生地址,如输入0视为保存原值");
                    String adress = sc.next();
                    String s="[4]"+id+","+name+","+age+","+adress;

                    Socket socket2 = new Socket("localhost", 8888);
                    OutputStream os2 = socket2.getOutputStream();
                    os2.write(s.getBytes());
                    socket2.shutdownOutput();
                    InputStream is2 = socket2.getInputStream();
                    byte[]bytes=new byte[1024];
                    int read2 = is2.read(bytes);
                    System.out.println(new String(bytes,0,read2));
                    socket2.close();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }


        }catch (Exception e){e.printStackTrace();}

    }

    private static void deleteStudent(Scanner sc) {
        try {
            Socket socket = new Socket("localhost", 8888);
            OutputStream os = socket.getOutputStream();
            System.out.println("请输入要删除的学生id");
            int id = sc.nextInt();
            os.write(("[3]"+id).getBytes());
            socket.shutdownOutput();

            InputStream is = socket.getInputStream();
           byte[]bytes=new byte[1024];
            int read = is.read(bytes);
            System.out.println(new String(bytes,0,read));
socket.close();
        }catch (Exception e){

        }
    }

    private static void findStudent() {
        try {
            Socket socket = new Socket("localhost", 8888);
            OutputStream os = socket.getOutputStream();
            os.write("[2]".getBytes());
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
           // System.out.println(list);
            for (Student student : list) {
                System.out.println(student);
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


        //读取

    private static void addStudent(Scanner sc) {
        System.out.println("请输入要添加的学员姓名");
        String name = sc.next();
        System.out.println("请输入要添加的学员年龄");
        int age = sc.nextInt();
        System.out.println("请输入要添加的学员地址");
        String adress = sc.next();
        String message="[1]"+name+","+age+","+adress;

        try {//写出任务
            Socket socket=new Socket("localhost",8888);
            socket.getOutputStream().write(message.getBytes());
            //读取
            byte[]bys=new byte[1024];
            int len = socket.getInputStream().read(bys);
            System.out.println(new String(bys,0,len));
            socket.close();
        }catch (Exception e){}

    }
}

Server

public class Test {
    public static void main(String[] args) {
        try {
            File file = new File("stu.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ois.close();
        } catch (Exception e) {
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
                ArrayList<Student> list = new ArrayList<>();
                oos.writeObject(list);
                oos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }


        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            while (true) {
                Socket accept = serverSocket.accept();
                new Thread(() -> {
                    try {
                        byte[] bytes = new byte[1024];
                        int len = accept.getInputStream().read(bytes);
                        String message = new String(bytes, 0, len);
                        char op = message.charAt(1);
                        switch (op) {
                            case '1':
                                addStudent(accept, message);//添加
                                break;
                            case '2':
                                findStudent(accept);//查询
                                break;
                            case '3':
                                deleteStudent(accept, message);//删除
                                break;
                            case '4':
                                updateStudent(accept, message);//修改
                                break;
                            case '5':
                                findOneStudent(accept, message);//查询一条
                                break;
                            default://关闭
                                System.out.println("格式错误");
                                accept.close();
                                break;
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();


            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void updateStudent(Socket accept, String message) {
        try {
            String[] split = message.substring(3).split(",");
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            Boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getId() == Integer.parseInt(split[0])) {
                    if (!"0".equals(split[1])) {
                        student.setName(split[1]);
                    }
                    if (!"0".equals(split[2])) {
                        student.setAge(Integer.parseInt(split[2]));
                    }
                    if (!"0".equals(split[3])) {
                        student.setAdress(split[3]);
                    }
                    flag = true;
                    break;
                }
            }
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
            oos.writeObject(list);
            oos.close();

            OutputStream os = accept.getOutputStream();
            if (flag == true) {
                os.write("修改成功".getBytes());
            } else {
                os.write("修改失败".getBytes());
            }
           accept.shutdownOutput();
            accept.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void findOneStudent(Socket accept, String message) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            Integer c = Integer.parseInt(message.substring(3));
            OutputStream os = accept.getOutputStream();
            Boolean flag = false;
            for (Student student : list) {
                if (student.getId() == c) {
                    flag = true;
                    break;
                }
            }
            if (flag == true) {
                os.write(1);
            } else {
                os.write(0);
            }
            ois.close();
            accept.close();
        } catch (Exception e) {
        }
    }


    private static void deleteStudent(Socket accept, String message) {
        try {
            String substring = message.substring(3);
            int id = Integer.parseInt(substring);
            ObjectInputStream os = new ObjectInputStream(new FileInputStream("stu.txt"));
            ArrayList<Student> list = (ArrayList<Student>) os.readObject();
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getId() == id) {
                    list.remove(i);
                    flag = true;
                    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
                    oos.writeObject(list);
                    break;
                }
            }
            OutputStream outputStream = accept.getOutputStream();
            if (flag == true) {
                outputStream.write("删除成功".getBytes());
            } else {
                outputStream.write("找不到要删除的ID,删除失败".getBytes());
            }
            accept.shutdownOutput();
            os.close();
            accept.close();

        } catch (Exception e) {
        }
    }

    private static void findStudent(Socket accept) {
        try {

            ObjectInputStream os = new ObjectInputStream(new FileInputStream("stu.txt"));
            ArrayList<Student> list = (ArrayList<Student>) os.readObject();
            ObjectOutputStream oos = new ObjectOutputStream(accept.getOutputStream());
            oos.writeObject(list);
            accept.shutdownOutput();
            accept.close();
        } catch (Exception e) {

        }
    }

    private static void addStudent(Socket accept, String message) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            String[] split = message.substring(3).split(",");
            Student student = new Student(++Utils.id, split[0], Integer.parseInt(split[1]), split[2]);
            list.add(student);

            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
            oos.writeObject(list);

            OutputStream os = accept.getOutputStream();
            os.write("添加成功".getBytes());
            os.close();
            accept.close();


        } catch (Exception e) {
        }
    }
}

Utils

public class Utils {
    public static int id=0;
    static {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"))) {
            ArrayList<Student>list = (ArrayList<Student>) ois.readObject();
                    if (list.size()==0){
                        id=0;
                    }else {
                        id = list.get(list.size() - 1).getId();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值