黑马程序员技术交流社区

标题: 【广州校区】+【原创】序列化+网络通信 [打印本页]

作者: 余大麻    时间: 2019-4-11 08:52
标题: 【广州校区】+【原创】序列化+网络通信
1 序列化概念Java自带序列化接口
[Java] 纯文本查看 复制代码
private static void decoding() throws Exception {
    ObjectInputStream oi = new ObjectInputStream(new FileInputStream("E:/test/obj1.txt"));
    Object object = oi.readObject();
    SerializableUser user = (SerializableUser) object;
    System.out.println(user);
    oi.close();
}


自定义序列化(FileOutputStream)将对象转成 json 格式再序列化
[Java] 纯文本查看 复制代码
private static void encoding2() throws Exception {
    SerializableUser user = new SerializableUser();
    user.set("9521", "周星星", 23, 10000D);
    Gson gson = new Gson();
    String json = gson.toJson(user);
    System.out.println(json);
    FileOutputStream fos = new FileOutputStream("E:/test/obj2.txt");
    fos.write(json.getBytes("utf-8"));
    fos.flush();
    fos.close();
}

[Java] 纯文本查看 复制代码
private static void decoding2() throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader((new FileInputStream("E:/test/obj2.txt"))));
    String json = br.readLine();
    br.close();
    Gson gson = new Gson();
    SerializableUser user = gson.fromJson(json, SerializableUser.class);
    System.out.println(user);
}

通过 DataOutputStream 优化上述代码
[Java] 纯文本查看 复制代码
private static void encoding3() throws Exception {
    SerializableUser user = new SerializableUser();
    user.set("9521", "周星星", 23, 10000D);
   
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("E:/test/obj3.txt"));
    dos.writeUTF(user.getId());
    dos.writeUTF(user.getName());
    dos.writeInt(user.getAge());
    dos.writeDouble(user.getSalary());
   
    dos.flush();
    dos.close();
}

[Java] 纯文本查看 复制代码
private static void decoding3() throws Exception {
    SerializableUser user = new SerializableUser();
   
    DataInputStream dis = new DataInputStream(new FileInputStream("E:/test/obj3.txt"));
    String id = dis.readUTF();
    String name = dis.readUTF();
    int age = dis.readInt();
    double salary = dis.readDouble();
    user.set(id, name, age, salary);
    System.out.println(user);
   
    dis.close();
}

总结2 服务概念
[AppleScript] 纯文本查看 复制代码
public class SocketClient1 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost",8888);
        OutputStream os = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        bw.write("Hello Dog!");
        bw.flush();
        bw.close();
    }
}

可以通过浏览器访问,打印出HTTP协议
[Java] 纯文本查看 复制代码
服务端启动...
服务端收到消息:
GET / HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8


web服务(restApi)3 进程与线程4 文件格式概念行式存储列式存储5 迭代器












欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2