利用IO流,Arraylist,网络通信实现班级管理系统(包括查看,增加,删除,修改,排序,保存,上传,下载)(目前下载功能还未实现原因是端口号报错)

本博客介绍了一个简单的学生信息管理系统,该系统支持基本的学生信息管理功能,如查看、添加、删除和修改学生信息,并能实现学生信息文件的上传与下载。

服务端

package IOliu.Work.Demo13;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class DemoTestRecive {
    final  static String drc="F:\\untitled199\\b.txt";  //服务器文件地址
    public static void main(String[] args) throws Exception {

            ServerSocket ss=new ServerSocket(10010);
            while (true){
                Socket s = ss.accept();
                new Thread(new ServeDemo(s)).start();

            }
    }

}
class  ServeDemo implements Runnable{
    private Socket socket;
    final  static String drc="F:\\untitled199\\b.txt";  //服务器文件地址
    public ServeDemo(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
              InputStream is=socket.getInputStream();
              FileOutputStream fos=new FileOutputStream(drc);
              int len;
              byte bys[]=new byte[1024];
              BufferedReader br=new BufferedReader(new InputStreamReader(is));
              String test=br.readLine();
              while ((len=is.read(bys))!=-1){
                  fos.write(bys,0,len);
              }
            OutputStream os = socket.getOutputStream();
              os.write("上传成功".getBytes());
              fos.close();
              socket.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

主界面

package IOliu.Work.Demo13;

//import ;

import com.sun.corba.se.spi.activation.Server;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class DemoTest {
    final static String src="F:\\untitled199\\a.txt";  //本地文件地址
    final static String drc="F:\\untitled199\\b.txt";  //服务器文件地址
    public static void main(String[] args) throws Exception {
         ArrayList<Student>  array1=new ArrayList <>();

        while (true) {
            System.out.println("-------欢迎来到学生管理系统-------");
            System.out.println("输入1查看学生");
            System.out.println("输入2添加学生");
            System.out.println("输入3删除学生");
            System.out.println("输入4修改学生");
            System.out.println("输入5排序学生");
            System.out.println("输入6保存学生到本地文件");
            System.out.println("输入7读取学生");
            System.out.println("输入8下载学生文件");
            System.out.println("输入9上传学生文件");
            System.out.println("输入其他退出系统");
            System.out.println("请输入你的选择");
            Scanner sc = new Scanner(System.in);

            String sh = sc.nextLine();
            switch (sh) {
                case "1":
                        showStudent(array1);
                    break;
                case "2":
                        addStudent(array1);
                    break;
                case "3":
                        deleteStudent(array1);
                    break;
                case "4":
                        updateStudent(array1);
                    break;
                case "5":
                        sortStudent(array1);
                    break;
                case "6":
                    writeData(array1);
//                    ReceilveStudent();
//                    SendStudent();
                    break;

                case  "7":
                    try {
                        array1=readData(array1);
                    }catch (EOFException e){
                        System.out.println("注意文件当前木有学生");
                    }catch (ClassNotFoundException e){
                        System.out.println("当前文件不存在,请先下载");
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
                case "8":
//                    returnStudent1();
//                    returnStudent2();
                    ReceiveStudent();
                    break;
                case "9":
                    SendStudent();
                    break;
                default:
                    System.out.println("谢谢使用");
                    System.exit(0);
                    break;
            }
         }
        }

    //从文件读入集合
    public static ArrayList<Student> readData(ArrayList<Student> array) throws Exception {
       ObjectInputStream ois=new ObjectInputStream(new FileInputStream("F:\\untitled199\\a.txt"));
       Object o=null;
           o = ois.readObject();
           ois.close();
           System.out.println("读取成功");

       return  (ArrayList<Student>)o;

    }
    //把集合中的数据写入文件
    public static void writeData(ArrayList<Student> array) throws IOException{
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(src));
        oos.writeObject(array);
        oos.close();
        System.out.println("保存成功");
    }
    public static void showStudent(ArrayList<Student> array){
    //首先判断这个集合是否有数据,并且让方法停止
    if (array.size()==0){
        System.out.println("不好意思先进行读取学生,如果已进行读取学生,可直接进行操作");
        return;
    }
    System.out.println("名字\t学号\t年龄\t地址");
    for (int i = 0; i < array.size(); i++) {
        Student student=array.get(i);
        System.out.println(student.getName()+"\t\t"+student.getId()+"\t\t"+student.getAge()+"\t\t"+student.getAddress());
    }
}
    public static void addStudent(ArrayList<Student> array){
        Scanner sc=new Scanner(System.in);
        Student stu=new Student();
        String id;
        while(true) {
            System.out.println("学号");
            id = sc.nextLine();
//        stu.setId(sc.nextLine());

            //判断学号是否被占用
            boolean flag = false;
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i).getId().equals(id)) {
                    flag = true;   //说明学号被占用
                }
            }
            if (flag == true) {
                System.out.println("已经有学号了请重新输入");
            }
            else {
                break;
            }
        }
        stu.setId(id);
        System.out.println("名字");
        stu.setName(sc.nextLine());
        System.out.println("年龄");
        stu.setAge(sc.nextLine());
        System.out.println("地址");
        stu.setAddress(sc.nextLine());
        array.add(stu);
        System.out.println("添加成功");
    }
    public static void deleteStudent(ArrayList<Student> array){
        Scanner sc=new Scanner(System.in);

        while (true) {
            System.out.println("请输入要删除的学生的学号");
            String id = sc.nextLine();
            boolean flag = false;
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i).getId().equals(id)) {
                    array.remove(i);
                    flag = true;
                    System.out.println("删除成功");
                    break;
                }
            }
            if (flag==false) {
                System.out.println("系统中木有这个学生请重写输入");
            }
            else {break;}
        }

    }
    public static void updateStudent(ArrayList<Student> array){
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改的学生的学号");
            String id = sc.nextLine();
            boolean flag = false;
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i).getId().equals(id)) {
                    array.remove(i);
                    addStudent(array);
                    flag = true;
                    System.out.println("修改成功");
                    break;
                }
            }
            if (flag==false) {
                System.out.println("系统中木有这个学生请重写输入");
            }
            else {break;}
        }
    }

    //排序
    public static void sortStudent(ArrayList<Student> array){
        Collections.sort(array);
    }
    // 上传文件
    public static void SendStudent() throws Exception {
//        Socket s=new Socket("DESKTOP-QG1LSJ2",9999);
//        OutputStream os = s.getOutputStream();
//        InputStream is=new FileInputStream(src);
//        int len;
//        while ((len=is.read())!=-1){
//            os.write(len);
//        }
//
//
//
//        System.out.println("-----1-----------");
//        BufferedInputStream bis=new BufferedInputStream(s.getInputStream());
//        int read;
//        OutputStream os1=System.out;
//
//        while ((read=bis.read())!=-1){
//            os1.write(read);
//        }
//        System.out.println("-----2-----------");
//        os1.close();
//        is.close();
//        s.close();
//        System.out.println("333333");
        Socket s=new Socket(InetAddress.getByName("DESKTOP-QG1LSJ2"),10010);
        //创建输出流6

        OutputStream os = s.getOutputStream();
        //发出数据
        FileInputStream fis=new FileInputStream("F:\\untitled199\\a.txt");

        int len;
        while ((len=fis.read())!=-1){
//            System.out.println(len);
            os.write(len);
        }
       s.shutdownOutput();  //关闭客户端输出流
        InputStream is = s.getInputStream();
        byte bys[]=new byte[1024];
        int read = is.read(bys);
        System.out.println(new String(bys,0,read));

//        //客户端接受数据
//        //创建输入流
//        InputStream is = s.getInputStream();
//        byte bys[]=new byte[1024];
//        int read;
//        //j接收数据
//        read=is.read(bys);
//        System.out.println(new String(bys,0,read));


        //释放资源
        fis.close();
        s.close();
//        System.out.println("上传成功");

    }
     //下载文件
    public static void ReceiveStudent() throws  Exception{
        Socket s=new Socket("127.0.0.1",10010);
        OutputStream os = s.getOutputStream();

        int len;
//        os.write("".getBytes());

        InputStream is = s.getInputStream();
        FileOutputStream fos=new FileOutputStream(src);
        while ((len=is.read())!=-1){
            fos.write(len);
        }
        fos.close();
        os.close();
        s.close();



    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值