**
**
package com.it.domain;
import java.io.Serializable;
//学生类
//必须实现这个接口才能被到文件中
public class Student implements Serializable{
//定义自动增加的编号
//这个值如果关闭系统会不会清0呢?如果清0的话不就出错了吗?
//把数据保存文件中就能达到重启服务器也不会重新重1开始
public static int idCount = 1;
//构造代码块
//在每次创建对象时会自动执行,每执行一个就会让编号加一
{
idCount++;
}
//成员变量
//编号
private int id;
//姓名
private String name;
//年龄
private int age;
//居住地
private String address;
//构造方法
public Student() {
}
public Student(int id, String name, int age, String address) {
//idCount++;
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
//setget
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;
}
//toString
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
package com.it.client;
import com.it.domain.Student;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class DemoCilent {
public static void main(String[] args) throws Exception {
//创建客户端对象
Socket s = new Socket("127.0.0.1",8888);
//让用户选择12345
while(true){
//给用户显示数据
System.out.println("请选择你要完成的操作:");
System.out.println("1. 增加学生 2.删除学生 3.修改学生 4.查看所有学生 5.退出");
System.out.println("--------------------------------------------------------");
//键盘录入
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
//判断用户选择的是什么操作
switch (choose){
case 1:
//增加学生
add(s);
break;
case 2:
//删除学生
remove(s);
break;
case 3:
//修改学生
update(s);
break;
case 4:
//查看所有学生
findAll(s);
break;
case 5:
//关闭资源
s.close();
//退出
return;
}
}
}
//修改方法
private static void update(Socket s) throws IOException {
//输入学生信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生编号");
int id = sc.nextInt();
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 str = "[3]"+id+","+name+","+age+","+address;
//发送
OutputStream out = s.getOutputStream();
out.write(str.getBytes());
//接受会送结果
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = in.read(bys);
//打印
System.out.println(new String(bys,0,len));
}
//查询所有方法
private static void findAll(Socket s) throws IOException, ClassNotFoundException {
//用户没有任何信心需要输入
//直接给服务器发送请求
OutputStream out = s.getOutputStream();
out.write("[4]".getBytes());
//接受服务器发回来的信息
//收取对象
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
//读取
ArrayList<Student> list = (ArrayList<Student>)ois.readObject();
//遍历集合打印数据
for (Student stu : list) {
System.out.println(stu);
}
}
//删除方法
private static void remove(Socket s) throws IOException {
//让用户输入删除的编号
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生编号:");
int id = sc.nextInt();
//给服务器发送数据
String str = "[2]"+id;
//发送
OutputStream out = s.getOutputStream();
out.write(str.getBytes());
//接受服务器发回的数据
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = in.read(bys);
//打印结果
System.out.println(new String(bys,0,len));
}
//添加方法
private static void add(Socket s) throws IOException {
//输入要添加的学生信息
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 str = "[1]"+name+","+age+","+address;
//获取输出流
OutputStream out = s.getOutputStream();
//把字符串转成字节数组发送数据
out.write(str.getBytes());
//--------------------------------------------
//接受服务器发回的数据
InputStream in = s.getInputStream();
//创建数组用于接受数据
byte[] bys = new byte[1024];
//读取
int len = in.read(bys);
//打印信息
System.out.println(new String(bys,0,len));
}
}
package com.it.server;
import com.it.domain.Student;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class DemoServer {
public static void main(String[] args) throws Exception {
//创建对象
ServerSocket ss = new ServerSocket(8888);
//在开启服务器的时候就提前创建一个文件
//判断文件是否存在,如果已存在就不创建 如果不存在就创建
File f = new File("day25-teacher\\user.txt");
if(!f.exists()) {
//在文件里面保存空集合
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day25-teacher\\user.txt"));
//创建集合
ArrayList<Student> list = new ArrayList<>();
//输出集合
oos.writeObject(list);
}
//循环
//连接多个客户端
while(true) {
//连接客户端
//阻塞方法
Socket s = ss.accept();
//连接上客户端之后给客户端开启一个线程
new Thread(new Runnable() {
@Override
public void run() {
//一个客户端的多次操作
while(true) {
try {
//处理客户端的操作
//接受客户端发来的数据
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
//读取
int len = in.read(bys);
String str = new String(bys, 0, len);
//获取用户的操作
char choose = str.charAt(1);
//获取信息
String message = str.substring(3);
//判断用户的操作
switch (choose) {
case '1':
//添加数据
add(message);
break;
case '2':
//删除数据
remove(message);
break;
case '3':
//修改数据
update(message);
break;
case '4':
//查询所有数据
findAll();
break;
}
} catch (Exception e) {
//打印异常信息
e.printStackTrace();
}
}
}
//修改方法
private void update(String message) throws IOException, ClassNotFoundException {
//拆分字符串
String[] arr = message.split(",");
//字符串形式的id转成整数
int id2 = Integer.parseInt(arr[0]);
//获取文件中的集合
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day25-teacher\\user.txt"));
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
//判断id是否存在
for (int i = 0; i < list.size(); i++) {
//获取集合中的学生
Student stu = list.get(i);
//获取学生的id
int id = stu.getId();
//如果id存在 修改成功
if(id == id2){
//判断各个值是否是0
if(!arr[1].equals("0"))
stu.setName(arr[1]);
if(!arr[2].equals("0"))
stu.setAge(Integer.parseInt(arr[2]));
if(!arr[3].equals("0"))
stu.setAddress(arr[3]);
//把信息更新到文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day25-teacher\\user.txt"));
oos.writeObject(list);
//给用户反馈
OutputStream out = s.getOutputStream();
out.write("修改成功".getBytes());
//结束方法
return;
}
}
//如果id不存在 修改失败
OutputStream out = s.getOutputStream();
out.write("修改失败".getBytes());
}
//查询所有
private void findAll() throws IOException, ClassNotFoundException {
//读取文件中的集合
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day25-teacher\\user.txt"));
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
//把集合用对象的方式发给客户端
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(list);
}
//删除方法
private void remove(String message) throws IOException, ClassNotFoundException {
//读取文件中的所有数据
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day25-teacher\\user.txt"));
//读取
ArrayList<Student> list = (ArrayList<Student>)ois.readObject();
//把字符串转成整数
int id2 =Integer.parseInt(message);
//获取输出流
OutputStream out = s.getOutputStream();
//判断集合中是否存在这个编号
for (int i=0; i<list.size();i++) {
//获取集合中的学生
Student stu = list.get(i);
//获取集合中的编号
int id = stu.getId();
//如果存在就删除并返回删除成功
if(id2 == id){
//删除对象
list.remove(i);
//给客户端会送删除成功
out.write("删除成功".getBytes());
//要更新文件
//对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day25-teacher\\user.txt"));
oos.writeObject(list);
//结束方法
return;
}
}
//如果不存在就返回编号不存在
out.write("编号不存在".getBytes());
}
//添加方法
private void add(String message) throws IOException, ClassNotFoundException {
//老王,40,隔壁
//切割
String[] strs = message.split(",");
//封装成一个学生对象
Student stu = new Student(Student.idCount,strs[0],Integer.parseInt(strs[1]),strs[2]);
//把对象保存到文件中
//对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day25-teacher\\user.txt"));
//读取已有的内容
ArrayList<Student> list = (ArrayList<Student>)ois.readObject();
//对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day25-teacher\\user.txt"));
//把新增的对象添加到集合中
list.add(stu);
//把集合输出到文件中
oos.writeObject(list);
//给用户会送响应
OutputStream out = s.getOutputStream();
out.write("添加成功".getBytes());
}
}).start();
}
}
}